C# - Using Reflection to Get Web Control Event Signatures

by Al Beecy January 8, 2009
I often find myself scratching around for event signature prototypes. Most documentation is dismally lacking in this regard, so today I decided to write a little bit of helper code that, given a control type, would spit out a list of all the event signatures.

The code below uses reflection to gather the needed info. I apologize in advance for the heavy use of dot notation that makes it difficult to see what reflection classes are being used to get the various bits of information, but I wanted to keep the code snippet as short as possible. I will post an expanded example in a future article that breaks it down when I have time.

The toughest part by far was figuring out how to get at the parameter info. It looks obvious now, but I wasted a couple hours trying to trick it into telling me that stuff. Hope this saves you a little time.

Below is just the code-behind of a standard ASP.Net web form. All you need to do is add a page to your web project and paste this over the auto-generated code. Note that you will need to change the class name (I named my page "ReflectEventSignatures", your page will probably be something like "_Default"). Note also that you must have the System.Text and System.Reflection namespaces referenced.

Then run the form and it will output all the signatures. Enjoy.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Text;
using System.Reflection;
 
public partial class ReflectEventSignatures : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //set these before running
        string controlName = "GridView1"; //just to make them pretty
        //change "GridView" to the type you are using
        Type t = typeof(GridView);
 
        //generate the signature for each event handler
        foreach (EventInfo ei in t.GetEvents())
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("protected ");
            //get event handler return type
            sb.Append(csType(ei.EventHandlerType.GetMethod("Invoke").ReturnType.Name));
            sb.Append(" ");
            //prepend control name followed by underscore
            sb.Append(controlName);
            sb.Append("_");
            //get the event name
            sb.Append(ei.Name);
            sb.Append("(");
            //get the parameters
            ParameterInfo[] pi = ei.EventHandlerType.GetMethod("Invoke").GetParameters();
            for (int i = 0; i < pi.Length; i++)
            {
                //get the type of the parameter
                sb.Append(csType(pi[i].ParameterType.ToString()));
                sb.Append(" ");
                //get the name of the parameter
                sb.Append(pi[i].Name);
                //add a comma if there are more
                if (i < pi.Length - 1)
                {
                    sb.Append(", ");
                }
            }
            sb.Append("){}");
            sb.Append("<br /><br />");
            Response.Write(sb.ToString());
        }
    }
 
    #region csType
    /// <summary>
    /// Helper function to massage CLR type
    /// descriptions into C# aliases.
    /// </summary>
    /// <param name="clrType">Type name</param>
    /// <returns>C# alias, if appropriate</returns>
    private string csType(string clrType)
    {
        switch (clrType)
        {
            case "System.String": return "string";
            case "System.SByte": return "sbyte";
            case "System.Byte": return "byte";
            case "System.Int16": return "short";
            case "System.UInt16": return "ushort";
            case "System.Int32": return "int";
            case "System.UInt32": return "uint";
            case "System.Int64": return "long";
            case "System.UInt64": return "ulong";
            case "System.Char": return "char";
            case "System.Single": return "float";
            case "System.Double": return "double";
            case "System.Boolean": return "bool";
            case "System.Decimal": return "decimal";
            case "System.Void": return "void";
            case "Void": return "void";
           
case "System.Object": return "object";

            default: return clrType;
        }
    }
    #endregion
}

Tags:

C# | Reflection

Comments

January 10, 2009 #

DotNetKicks.com

Trackback from DotNetKicks.com

Using Reflection to Get Web Control Event Signatures

DotNetKicks.com

January 10, 2009 #

Web Development Community

Trackback from Web Development Community

C# - Using Reflection to Get Web Control Event Signatures

Web Development Community

June 15, 2009 #

DotNetShoutout

C# - Using Reflection to Get Web Control Event Signatures

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

Powered by BlogEngine.NET1.5.0.7 | Theme by Mads Kristensen