Wednesday, February 19, 2014

Reflections in C# ( A Simple Process To Get Info About an Assembly ) Assemblies Dealings




Make a program by using concept of reflection in which we get the type related to object.


HelpingClass.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reflectionlibrary
{
enumColorEnum
    {
        red = 1,
        green = 2,
        blue = 3
    }
interfaceIBase
    {
String Method1(string a);
double Calculate(int a, double b);
    }
publicclassClass1 : IBase
    {
String name = "Muhammad Abdullah";
staticint rate = 10;
public Class1()
        {
Console.WriteLine("Class1 Implemented");
        }
publicstring Method1(string a)
        {
Console.WriteLine("Method1 Called");
return name;
        }
publicdouble Calculate(int a, double b)
        {
Console.WriteLine("Calculate Called");
return a * b;
        }
structs
    {
int a;
string b;
publicvoid same()
        {
            a = 11;
            b = "Muhammad Abdullah";
        }  }





Form1.cs:

using System;
using System.Windows.Forms;
using System.Reflection;
namespace reflection_labtask
{
publicpartialclassReflection : Form
    {
Type[] t;
MethodInfo[] m;
ParameterInfo[] p;
ConstructorInfo[] c;
public Reflection()
        {
            InitializeComponent();
        }
privatevoid Form1_Load(object sender, EventArgs e)
        {Assembly as1 = Assembly.LoadFrom("reflectionlibrary.dll");
            t = as1.GetTypes();
foreach (Type t1 in t)
            {type.Items.Add(t1.Name);}
        }
privatevoid type_SelectedIndexChanged(object sender, EventArgs e)
        {int a = type.SelectedIndex;
            m = t[a].GetMethods();
            namespac.Text = t[a].Namespace;
if (t[a].BaseType != null)
            {basee.Text =Convert.ToString( t[a].BaseType);}
Else basee.Text = "No Base type";
            c = t[a].GetConstructors();
foreach (ConstructorInfo ci in c)
            {constructures.Items.Add(ci);}
            methodd.Items.Clear();
foreach (MethodInfo me in m)
            {
                methodd.Items.Add(me.Name);
            }
        }
privatevoid methodd_SelectedIndexChanged(object sender, EventArgs e)
        {
int o = methodd.SelectedIndex;
            p = m[o].GetParameters();
            parameter.Items.Clear();
            datatype.Items.Clear();
foreach (ParameterInfo par in p)
            {parameter.Items.Add(par.Name);
                datatype.Items.Add(par.ParameterType.Name);
            } }
privatevoid label5_Click(object sender, EventArgs e)
        {} } }



No comments:

Post a Comment