Thursday, June 24, 2010

Get the value of static variable using reflection

I come to this code when i want to create new company for my application when new company is created i want to create whole folder structure again for uploading their documents or images with their company folder name.

Code

public static void CreateCorporateStructure(String _foldername)
    {
        try
        {
            Type typeOfclsConstant = typeof(clsConstant);
            FieldInfo[] objFieldInfo = typeOfclsConstant.GetFields();
            foreach (FieldInfo fi in objFieldInfo.Where(fi => fi.Name.Contains("_POSTFIX")))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath("../" + clsConstant.COMPANY_PREFIXURL) + _foldername + Convert.ToString(fi.GetValue(null)).Substring(0, Convert.ToString(fi.GetValue(null)).Length - 1));
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Explaination

  • foreach (FieldInfo fi in objFieldInfo.Where(fi => fi.Name.Contains("_POSTFIX"))) is used to fetch number of fields from static class where variable name is ending with "_POSTFIX".
  • fi.GetValue(null)) which give the static value for field or variable.
Give some comment for above code if it is useful. If you find any suggestion than please reply.

Microsoft Ajax Editor Control With ToolsFile

I come to ajxt toolkit editor control and find that there's no option or property like radeditor where we can define the path of ToolsFile to give dynamic look to toolbars.

I try to implement a extender control for Editor which having the property ToolsFile

Example:

Extender Control
------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AjaxControlToolkit.HTMLEditor;
using System.ComponentModel;
using System.Web.UI;
using System.Xml.Linq;
using System.Web;
using System.Reflection;
using AjaxControlToolkit.HTMLEditor.ToolbarButton;
using System.Collections.ObjectModel;

namespace SazzControls
{
    public class editor : Editor
    {
        ///
        /// Created by Asif Ghanchi Dated On June 21 2010
        ///

        #region " Local Variable "
        #endregion
        #region " Properties"

        [Description("Relative path of Tools File"), Category("Appearance"), Bindable(BindableSupport.Yes),
        UrlProperty("*.xml")]
        public string ToolsFile
        {
            get
            {
                return (Convert.ToString(ViewState["ToolsFile"]) == null) ? string.Empty : Convert.ToString(ViewState["ToolsFile"]);
            }
            set
            {
                ViewState["ToolsFile"] = value;
            }
        }

        #endregion
        #region " Constructor "
        #endregion
        #region " Control Methods "

        protected override void FillTopToolbar()
        {
            if (ToolsFile != string.Empty)
            {
                XDocument xd = XDocument.Load(HttpContext.Current.Server.MapPath(ToolsFile));
                Assembly asm = Assembly.GetAssembly(typeof(AjaxControlToolkit.HTMLEditor.ToolbarButton.CommonButton));

                foreach (XElement tool in xd.Descendants("tool"))
                {
                    Type t = asm.GetTypes().FirstOrDefault(cb => cb.Namespace == "AjaxControlToolkit.HTMLEditor.ToolbarButton" && cb.Name == tool.FirstAttribute.Value);
                    if (t != null)
                    {
                        CommonButton _tool = (CommonButton)Activator.CreateInstance(t);
                        TopToolbar.Buttons.Add(_tool);
                        foreach (XElement option in tool.Descendants("option"))
                        {
                            Collection opts = null;
                            AjaxControlToolkit.HTMLEditor.ToolbarButton.SelectOption opt;
                            if (tool.FirstAttribute.Value == "FontName") opts = ((FontName)_tool).Options;
                            else if (tool.FirstAttribute.Value == "FontSize") opts = ((FontSize)_tool).Options;
                            opt = new AjaxControlToolkit.HTMLEditor.ToolbarButton.SelectOption();
                            opt.Value = option.Attribute("value").Value;
                            opt.Text = option.Attribute("text").Value;
                            opts.Add(opt);
                        }
                    }
                }
            }
        }

        #endregion
        #region " Methods "
        #endregion
        #region " Enum "
        #endregion
    }
}

---------------------------
XML File

download
--------------------------------

Please give comments for above if you having any query or suggestion regarding this.