Using Reflection to detemine as Assembly Info in and out.

Sample article that describes how easy it is to determine the assembly info in and out. The assembly holds a vital information and can also be retrieved during runtime using reflection. Some of the information that can be retrieved are: 1. Assembly attributes - Type, BaseType, type of class, is it sealed, is it abstract. e.t.c 2. Base attributes 3. Properties defined.

Note: I have removed the designer and most other relevant code to make it clean.
Controls required:
1. a Listbox called "listTypeInfo" ,
2. a button called "GetAssemblyInfo" and an event handler attached to its click event (btnGetInfo_Click)
3. a combobox called 'cmbAssemblies'


Also note:
1. I have used three assemblies called "Broker", "Trader", and "Custodian assembly"
2. In the form load event, the assemblies are loaded.
3. In the getInfo button click, the rest of the info is retreived.


I have added necessary comments , otherwise



CODE:

using
System;
using System.Reflection;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Reflection
{
    /// <summary>
    /// Summary description for ReflectionForm.
    /// </summary>
    public class ReflectionForm : System.Windows.Forms.Form
    {
        //Designer Code

        /// <summary>
        /// traderTypes.
        /// </summary>
        private Type[] traderTypes;

        /// <summary>
        /// brokerTypes.
        /// </summary>
        private Type[] brokerTypes;

        /// <summary>
        /// custodianTypes.
        /// </summary>
        private Type[] custodianTypes;

        public ReflectionForm()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

         /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
             if( disposing )
             {
                 if (components != null)
                 {
                     components.Dispose();
                 }
             }
             base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
         /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
          //Designer Code
        }
         #endregion

        /// <summary>
        /// Handles the Load event of the Form control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Form_Load(object sender, System.EventArgs e)
        {
            //Load the Assemblies

             //I have 3 types of dll's
            //1: Trader
            //2: Broker
            //3: Custodian

            Assembly traderAssembly = Assembly.LoadFrom("..\\..\\trader.dll");
            traderTypes = traderAssembly.GetTypes();

            Assembly brokerAssembly = Assembly.LoadFrom("..\\..\\broker.dll");
            brokerTypes = brokerAssembly.GetTypes();

            Assembly custodianAssembly = Assembly.LoadFrom("..\\..\\custodian.dll");
            custodianTypes = custodianAssembly.GetTypes();

             //Select the initial index.
            cmbAssemblies.SelectedIndex = 0;

        }

        /// <summary>
        /// Handles the Click event of the btnGetInfo control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnGetInfo_Click(object sender, System.EventArgs e)
        {
            //get the name of the assembly selected by the user.
            string SelectedAssembly = cmbAssemblies.SelectedItem.ToString();
             
             //call the LoadTypeInfo into an assembly array.
            if (SelectedAssembly == "trader")
            {
                 listTypeInfo.Items.Clear();
                 LoadTypeInfo(traderTypes);
             }
             else if (SelectedAssembly == "broker")
            {
                 listTypeInfo.Items.Clear();
                 LoadTypeInfo(brokerTypes);
             }
             else if (SelectedAssembly == "custodian")
            {
                 listTypeInfo.Items.Clear();
                 LoadTypeInfo(custodianTypes);
             }
             else
            {
                 //Just clear the listTypes.
                 listTypeInfo.Items.Clear();
             }

        }

        /// <summary>
        /// Loads the type info.
        /// </summary>
        /// <param name="AssemblyType">Type of the assembly.</param>
        private void LoadTypeInfo(Type[] AssemblyType)
         {
             /*There are various types of info that you can get through the AssemblyType.
             * 1. Assembly attributes   - Type, BaseType, type of class, is it sealed, is it abstract. e.t.c
             * 2. Base attributes      
             * 3. Properties defined.
            */

             //Parse the AssemblyType info to load the other information related to the assembly.
            //the Attributes are written to the list box.
            foreach(Type baseType in AssemblyType)
             {
                 #region AssemblyAttributes
                 
                 //get the full name of the assembly.
                 listTypeInfo.Items.Add("Info:Assembly Type: "+baseType.FullName+"("+baseType.UnderlyingSystemType+")");
                 //get the base type of the assembly.
                 listTypeInfo.Items.Add("Info:Base Type: " + baseType.BaseType);
                 //is the assembly serializable.
                 listTypeInfo.Items.Add("Info:Serializable: " + baseType.IsSerializable);
                 //is the assembly defined as abstract.
                 listTypeInfo.Items.Add("Info:Abstract: " + baseType.IsAbstract);
                 //is the assembly a class library.
                 listTypeInfo.Items.Add("Info:Class: " + baseType.IsClass);
                 //is the assembly public.
                 listTypeInfo.Items.Add("Info:Public: " + baseType.IsPublic);
                 //is the assembly sealed.
                 listTypeInfo.Items.Add("Info:Sealed: " + baseType.IsSealed);

                 #endregion AssemblyAttributes

                 #region BaseAttributes
                 //get the base attributes.
                System.Reflection.TypeAttributes baseTypeAttributes = baseType.Attributes;

                 listTypeInfo.Items.Add("");
                 listTypeInfo.Items.Add("Info:Attributes:");
                 listTypeInfo.Items.Add(baseTypeAttributes.ToString());

                 #endregion BaseAttributes

                 #region PropertyInfo

                 //get all of the properties defined on the assembly.
                PropertyInfo[] propInfo = baseType.GetProperties();
                 
                  listTypeInfo.Items.Add("Info:Properties:");
                 
                 //loop through the property info array.
                 foreach(PropertyInfo prop in propInfo)
                 {
                     //returns the property name.
                     listTypeInfo.Items.Add("Info:Name: " + prop.Name);
                     //returns the member type "Property".
                     listTypeInfo.Items.Add("Info:Member Type: " + prop.MemberType);
                     //returns the property type.
                     listTypeInfo.Items.Add("Info:Property Type: " + prop.PropertyType);
                     //bool property that says whether the property have a get method defined.
                     listTypeInfo.Items.Add("Info:Read: " + prop.CanRead);
                     //bool property that says whether the property have a set method defined.
                     listTypeInfo.Items.Add("Info:Write: " + prop.CanWrite);
                     listTypeInfo.Items.Add("");
                 }

                 #endregion PropertyInfo

                 #region MethodInfo

                 //returns all of the methods of the assembly.
                MethodInfo[] methodInfo = baseType.GetMethods();

                 listTypeInfo.Items.Add("");
                 listTypeInfo.Items.Add("");
                 listTypeInfo.Items.Add("Info:Methods:");
                 
                 //loop through the method info array.
                 foreach(MethodInfo method in methodInfo)
                 {
                     //returns the method name.
                     listTypeInfo.Items.Add("Info:Name: " + method.Name);
                     //returns the membertype.
                     listTypeInfo.Items.Add("Info:Member Type: " + method.MemberType);
                     //returns the return type of the method.
                     listTypeInfo.Items.Add("Info:Return Type: " + method.ReturnType);
                     //bool property that says whether the method defined as public.
                     listTypeInfo.Items.Add("Info:Public: " + method.IsPublic);
                     //bool property that says whether the method defined as static.
                     listTypeInfo.Items.Add("Info:Static: " + method.IsStatic);
                     //bool property that says whether the  method defined as private.
                     listTypeInfo.Items.Add("Info:Private: " + method.IsPrivate);
                     //bool property that says whether the method defined as abstract.
                     listTypeInfo.Items.Add("Info:Abstract: " + method.IsAbstract);
                     //bool property that says whether the  method defined as virtual.
                     listTypeInfo.Items.Add("Info:Virtual: " + method.IsVirtual);
                     listTypeInfo.Items.Add("");
                 }

                 #endregion MethodInfo
            }

        }
        
        
     }
}
By [)ia6l0 iii   Popularity  (1324 Views)