Double the .NET program written by others, but there is only one compiled exe ---- Reflection (reflection)

xiaoxiao2021-03-06  85

If you want to write the .NET program written by others, only one compiled exe is in your hand, one is to use Reflection. Here are some examples, people who have started players can be referred to.

First assume that the third-party application exe we have to be multiplexed is compiled by the following code:

Using system;

Namespace mynamespace

{

Public class myapp

{

Public mynestedObject mynestedObject = NULL;

Public class mynestedObject {

Public String Name;

}

Public Enum foodsEasonSenum {

Spring, Summer, Autumn, Winter

}

Public myapp () {

}

Public myApp (MyNamespace.MYFORM FORM) {

}

Public mynestedObject foo1 (foursasonsenum season) {

Return this.mynestedObject;

}

Public string foo2 () {

""; "

}

Static void main () {

}

}

Public Class MyForm

{

Public myform () {

}

}

}

Here are some of the frequent calls that are directly referenced to how to use reflection to rewrite:

1. Generate an object with constructor without parameters

Directly quote, the code is

MyNameSpace.myapp app = new mynamespace.myapp ();

If you call it with Reflection, you need this to write (remember using system.reflection)

AskMBLY Assem = askEMBLY.LOADFILE (@ "c: /Home/Workspace/mynamespace/myapp.exe");

TYPE myApptype = askEM.GETTYPE ("MyNameSpace.myApp");

Constructorinfo myapptype_constructor = myapptype.getconstructor (new type [] {};

Object App = myApptype_constructor.invoke (new object [] {};

2. Generate objects with parametric constructor

Directly quote, the code is

MyNameSpace.myapp app = new mynamespace.myapp (new mynamespace.myform ());

If you call it with reflection, you need to write.

AskMBLY Assem = askEMBLY.LOADFILE (@ "c: /Home/Workspace/mynamespace/myapp.exe");

TYPE myApptype = askEM.GETTYPE ("MyNameSpace.myApp");

TYPE myFormType = askEM.GETTYPE ("MyNameSpace.MYFORM");

Constructorinfo myapptype_constructor = myapptype.getconstructor (new type [] {myFormTyPE});

Constructorinfo myformtype_constructor = myFormType.getConstructor (new type [] {};

Object form = myFormType_constructor.invoke (new object [] {};

Object app = myapptype_constructor.invoke (new object [] {form}; 3. Method for calling objects

Directly quote, the code is

MyNameSpace.myapp app = new mynamespace.myapp ();

String str = app.foo2 ();

If you call it with reflection, you need to write.

AskMBLY Assem = askEMBLY.LOADFILE (@ "c: /Home/Workspace/mynamespace/myapp.exe");

TYPE myApptype = askEM.GETTYPE ("MyNameSpace.myApp");

Constructorinfo myapptype_constructor = myapptype.getconstructor (new type [] {};

Object App = myApptype_constructor.invoke (new object [] {};

Object str = myApptype.getMethod ("foo2"). Invoke (app, new object [] {});

4. SET / GET member variable

Directly quote, the code is

MyNameSpace.myapp app = new mynamespace.myapp ();

MyNamespace.myApp.mynestedObject obj = app.mynestedObject;

MyNamespace.myapp.mynestedObject Obj2 = new mynamespace.myapp.mynestedObject ();

app.mynestedObject = Obj2;

If you call it with reflection, you need this to write (note that the MynestedObject class here is NESTED TYPE, the name is "MyNameSpace.myApp mynestedObject" instead of "MyNameSpace.myApp.mynestedObject")

AskMBLY Assem = askEMBLY.LOADFILE (@ "c: /Home/Workspace/mynamespace/myapp.exe");

TYPE myApptype = askEM.GETTYPE ("MyNameSpace.myApp");

Constructorinfo myapptype_constructor = myapptype.getconstructor (new type [] {};

Object App = myApptype_constructor.invoke (new object [] {};

Type mynestedObjectType = askEM.GETTYPE ("MyNameSpace.myApp MynestedObject");

FieldInfo mynestedobjfield = myapptype.getfield ("mynestedObject");

Object obj = mynestedobjfield.getValue (app);

Constructorinfo mynestedObjectType_constructor = mynestedObjectType.getConstructor (new type [] {};

Object obj2 = mynestedObjectType_constructor.invoke (new object [] {});

MynestedObjfield.SetValue (app, obj2); 5. Use enumeration type

Directly quote, the code is

MyNameSpace.myapp app = new mynamespace.myapp ();

MyNameSpace.myapp.mynestedObject obj = app.foo1 (mynamespace.myapp.fourseeasonsenum.spring);

If you call it with reflection, you need to write.

AskMBLY Assem = askEMBLY.LOADFILE (@ "c: /Home/Workspace/mynamespace/myapp.exe");

TYPE myApptype = askEM.GETTYPE ("MyNameSpace.myApp");

Constructorinfo myapptype_constructor = myapptype.getconstructor (new type [] {};

Object App = myApptype_constructor.invoke (new object [] {};

TYPE FOURSONSENUMTYPE = Assem.gettype ("MyNameSpace.myApp FourseSonsenum");

Array foursasonsenumvalues ​​= enum.getvalues ​​(foursasonsenumtype);

Object SpringValue = foursasonsenumvalues.getValue (0);

Object results = myApptype.getMethod ("foo1"). Invoke (app, new object [] {springValue});

---

Finally, as long as it is possible, try not to use REFLECTION, because it is directly called, the Reflection has a fair performance.

转载请注明原文地址:https://www.9cbs.com/read-105954.html

New Post(0)