Tuesday, October 6, 2009

Reflaction and example

Reflection

To read method, prop, construtor, etc from assembly.

We can create assembly at run time

When we use c# dll in vb, and two variables a and A can also be used using reflection

Create new class liabrary (new project in c#)

Public class class1

{

Private int32 eno , es;

Private string en,ed;

Public class1()

{

}

Public class1 ( int32 a, string b, string c, int32 d)

{

Eno = a; ed= c; en=b; es = d;

}

Public int32 p_empno

{

Get

{

Return eno;

}

Set

{

Eno =value;

}

}

Public string p_ename

{

Get

{

Return en;

}

Set

{

En =value;

}

}

Public string p_eadd

{

Get

{

Return ed;

}

Set

{

Ed =value;

}

}

Public int32 p_esal

{

Get

{

Return es;

}

Set

{

Es = value;

}

}

Public int32 callall()

{

Return p_sal * 10 /100;

}

Public int32 calded(int32 pf, int32 tax)

{

Return p_esal * pf/100 + p_esal * tax / 100;

}

}

Compile this class file

Create new web site in c#

Default.aspx;

Code

Using System.reflection;

Type typ = null;

Pageload()

{

Assembly asm = assembly.loadfile(“d:\\classliabrary24 \\classlibrary24\\bin\\debug\\classlibrary24.dll”); //path of dll file

Type[] typs = asm.gettypes; //element of class may be more than one

Typ = asm.gettype(typs[0].tostring());

}

Design

Button1 button2 button3 button4

Button1()

{

Constructorinfo[] ci = typ.getconstructors();

//all constructors in class lib

Int32 I;

For(i=0; i<>

{

Response.write(ci[i].tostring() + “
”);

}

}

Button2()

{

Propertyinfo[] pi = typ.getproperty();

Int32 I;

For(i=0;i

{

Resoponse.write(pi[i].tostring() + “
”);

}

}

Button3()

{

Methodinfo[] mi = typ.getmethods();

Int32 I;

For(i=0;i

{

Response.write(mi[i].tostring() + “
”);

}

}

Button4()

{

Constructorinfo[] ci = typ.getconstructors();

Object obj = activator.createinstance(typ);

//Ci[0].invoke(obj,null); //calling default constructor

Object[] ar = new object[4];

Ar[0] = 10;

Ar[1]=”vikas”;

Ar[2]=”#2223”;

Ar[3]=12000;

Ci[1].invoke(obj,ar);

Propertyinfo[] pi = typ.getproperties();

Response.write(pi[0].getvalue(obj,null).tostring() + “
”);

//to read from property getvalue used

Response.write(pi[1].getvalue(obj,null).tostring() + “
”);

Response.write(pi[2].getvalue(obj,null).tostring() + “
”);

}

Add one more button5

Button5()

{

Propertyinfo[] pi = typ.getproperties();

Object obj = activator.createinstanse(typ);

Pi[0].setvalue(obj,101,null);

Pi[1].setvalue(obj,”aman”,null);

Pi[2].setvalue(obj,”#23adsf”,null);

Pi[3].setvalue(obj,23000,null);

Methodinfo[] mi = typ.getmethods();

Int32 k = (int32)(mi[8].invoke(obj,null));

Response.write(k.tostring() + “
”);

Object[] ar = new object[2];

Ar[0]=20;

Ar[1]=30;

Int32 k1 (int32)(mi[9].invoke(obj,ar));

Response.write(k1.tostring());

}

Add new page:




Dll at runtime:

--------------

Default2.aspx:

Textbox1 Button1

Code:

Using system.reflection;

Using system.reflection.emit;

//emit namespace is used for

Pageload()

{

}

Private assembly abc()

{

Assemlyname nam = new assemblyname();

Nam.name = “myfirstasm”;

Assemblybuilder asmbul = appdomain.currentdomain. definedynamicassembly(nam,assemblybuilderaccess.runandsave,”e:\\abc”);

//current area of app where it is running

//assemblybuilderaccess is used for the type of assmb at runtime, run save or both

//very useful for security purpose

Modulebuilder modbul = asmbul.definedynamicmodule (“myfirstasm”,”myfirstasm.dll”);

// for making namespace

Typebuilder typbul = modbul.definetype(“clsabc”,typeattributes.public | typeattributes.class);

//for making class

Type[] ar = new type[]{typeof(int32),typeof(int32)};

//prototype of a method

Methodbuilder metbul = typbul.definemethod(“sum”,methodattribute.public, typeof(int32), ar);

// for making runtime method

Parameterbuilder p1 = metbul.defineparameter(1, parameterattributes.in, “n1”);

Parameterbuilder p2 = metbul.defineparameter(2, parameterattributes.in, ”n2”);

Ilgenerator ilgen = metbul.getilgenerator();

//for generating msil

Ilgen.emit(opcodes.lgarg_1);

//ldarg_1 stores in stack

Ilgen.emit(opcodes.ldarg_2);

Ilgen.emit(opcodes.add);

Ilgen.emit(opcodes.ret);

Typbul.createtype();

Asmbul.save(“myfirstasm.dll”);

Return asmbul;

}

Button1()

{

Assembly asm = abc();

Type typ = asm.gettype(“clsabc”);

Object obj = activator.createinstance(typ);

Object[] ar = new object[2];

Ar[0]=350;

Ar[1]=500;

Int32 k = (int32) ( typ.invokemember(“sum”,bindingflags.invokemethod,null, obj, ar);

//name of method, invoking method, overloading situation, object name, parametr

Textbox1.text = k.tostring();

}

No comments:

Post a Comment