Demonstrate how to gather information on various types included in any assembly by using the System.Reflection namespace and some main.NET base classes. - Code

Latest

programs

Thursday, October 6, 2011

Demonstrate how to gather information on various types included in any assembly by using the System.Reflection namespace and some main.NET base classes.


Aim:-  Demonstrate how to gather information on various types included in any assembly by using the System.Reflection namespace and some main.NET base classes.

Source code:-
Customer. Class

using System;
namespace Customer
{
      public class Client
      {
            public Client()
            {
            }
            public string GetClient(String ClientID)
            {
                  return "ID is blank";
            }
      }
}

worker.class

using System;
namespace Employee
{
      public class Worker
      {
            public Worker()
            {
            }
            public string GetWorker(String WorkerID)
            {
                  return "ID is blank";
            }
      }
}

individual.class

using System;
namespace Person
{
      public class Individual
      {
            public Individual()
            {
            }
            public string GetIndividual(String IndividualID)
            {
                  return "ID is blank";
            }
      }
}


reflectionform.cs

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

namespace Reflection
{
      public class ReflectionForm : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button GetAssemblyInfo;
            private System.Windows.Forms.ComboBox listAssemblies;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.ListBox listTypes;
            private System.ComponentModel.Container components = null;
            private Type[] PersonTypes;
            private Type[] EmployeeTypes;
            private Type[] CustomerTypes;

            public ReflectionForm()
            {
                  InitializeComponent();
            }
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {if (components != null)
                        {
                        components.Dispose();
                  }
                  base.Dispose( disposing );
            }
            #region Windows Form Designer generated code
            private void InitializeComponent()
            {
            this.GetAssemblyInfo = new System.Windows.Forms.Button();
            this.listAssemblies = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.listTypes = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
     this.GetAssemblyInfo.Location = new System.Drawing.Point(280, 40);
            this.GetAssemblyInfo.Name = "GetAssemblyInfo";
           this.GetAssemblyInfo.Size = new System.Drawing.Size(75, 23);
            this.GetAssemblyInfo.TabIndex = 0;
            this.GetAssemblyInfo.Text = "Get Info";
        this.GetAssemblyInfo.Click += new 
                         System.EventHandler(this.btnGetInfo_Click);
     DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.listAssemblies.Items.AddRange(new object[] {
            "Person", "Employee",  "Customer"});
    this.listAssemblies.Location = new System.Drawing.Point(144, 40);
     this.listAssemblies.Name = "listAssemblies";
    this.listAssemblies.Size = new System.Drawing.Size(121, 21);
    this.listAssemblies.TabIndex = 1;
    this.listAssemblies.SelectedIndexChanged += new       System.EventHandler(this.cboAssemblies_SelectedIndexChanged);
   this.label1.Location = new System.Drawing.Point(16, 40);
  this.label1.Name = "label1";
  this.label1.Size = new System.Drawing.Size(112, 16);
  this.label1.TabIndex = 2;
  this.label1.Text = "Select An Assembly: ";
  this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  this.listTypes.Location = new System.Drawing.Point(8, 88);
  this.listTypes.Name = "listTypes";
  this.listTypes.Size = new System.Drawing.Size(376, 459);
  this.listTypes.TabIndex = 3;
  this.listTypes.SelectedIndexChanged += new System.EventHandler(this.listTypes_SelectedIndexChanged);
  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  this.ClientSize = new System.Drawing.Size(479, 559);
  this.Controls.Add(this.listTypes);
  this.Controls.Add(this.label1);
  this.Controls.Add(this.listAssemblies);
  this.Controls.Add(this.GetAssemblyInfo);
  this.Name = "ReflectionForm";
  this.Text = "Reflection Sample";
  this.Load += new System.EventHandler(this.ReflectionForm_Load);
  this.ResumeLayout(false);
            }
            #endregion
            static void Main()
            {Application.Run(new ReflectionForm());
            }
private void btnGetInfo_Click(object sender, System.EventArgs e)
            {
string SelectedAssembly = this.listAssemblies.SelectedItem.ToString();
                  if(SelectedAssembly == "Person")
                  {
                  listTypes.Items.Clear();
                  LoadTypeInfo(PersonTypes);
                  }
                  else if (SelectedAssembly == "Employee")
                  {
                  listTypes.Items.Clear();
                  LoadTypeInfo(EmployeeTypes);
                  }
                  else if (SelectedAssembly == "Customer")
                  {
                        listTypes.Items.Clear();
                        LoadTypeInfo(CustomerTypes);
                  }
                  else
                  {
                        listTypes.Items.Clear();
                  }

            }      
private void LoadTypeInfo(Type[] AssemblyType)
{
foreach(Type baseType in AssemblyType)
{
listTypes.Items.Add("Assembly Type: "+baseType.FullName+"
                       ("+baseType.UnderlyingSystemType+")");
listTypes.Items.Add("Base Type: "+baseType.BaseType);
listTypes.Items.Add("Serializable: " + baseType.IsSerializable);
listTypes.Items.Add("Abstract: " + baseType.IsAbstract);
listTypes.Items.Add("Class: " + baseType.IsClass);
listTypes.Items.Add("Public: " + baseType.IsPublic);
listTypes.Items.Add("Sealed: " + baseType.IsSealed);
System.Reflection.TypeAttributes baseTypeAttributes=
                            baseType.Attributes;                                                   listTypes.Items.Add("");
listTypes.Items.Add("Attributes:");
listTypes.Items.Add(baseTypeAttributes.ToString());
PropertyInfo[] propInfo = baseType.GetProperties();
MethodInfo[] methodInfo = baseType.GetMethods();
listTypes.Items.Add("");           
listTypes.Items.Add("Properties:");
foreach(PropertyInfo prop in propInfo)
{
listTypes.Items.Add("Name: " +prop.Name);
listTypes.Items.Add("Member Type: " +prop.MemberType);
listTypes.Items.Add("Property Type: " +prop.PropertyType);
listTypes.Items.Add("Read: " +prop.CanRead);
listTypes.Items.Add("Write: " +prop.CanWrite);
listTypes.Items.Add("");
}
listTypes.Items.Add("");
listTypes.Items.Add("Methods:");
foreach(MethodInfo method in methodInfo)
{
listTypes.Items.Add("Name: " +method.Name);
listTypes.Items.Add("Member Type: " +method.MemberType);
listTypes.Items.Add("Return Type: " +method.ReturnType);
listTypes.Items.Add("Public: " +method.IsPublic);
listTypes.Items.Add("Private: " +method.IsPrivate);
listTypes.Items.Add("Abstract: " +method.IsAbstract);
listTypes.Items.Add("Virtual: " +method.IsVirtual);
listTypes.Items.Add("Static: " +method.IsStatic);
listTypes.Items.Add("");
      }
    }
}
private void ReflectionForm_Load(object sender, System.EventArgs e)
{
Assembly PersonAssembly = Assembly.LoadFrom("..\\..\\Person.dll");
PersonTypes = PersonAssembly.GetTypes();
Assembly EmployeeAssembly = Assembly.LoadFrom("..\\..\\Employee.dll");
EmployeeTypes = EmployeeAssembly.GetTypes();
Assembly CustomerAssembly = Assembly.LoadFrom("..\\..\\Customer.dll");
CustomerTypes = CustomerAssembly.GetTypes();
listAssemblies.SelectedIndex=0;
}
private void cboAssemblies_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void listTypes_SelectedIndexChanged(object sender, EventArgs e) {
}
      }
}
Output:-



Result:- The  programme is executed successfully without  any errors.

No comments:

Post a Comment