Dynamic call of Class Members Using Reflection

Source: Internet
Author: User

If you want to reuse the. NET file written by othersProgramBut there is only one compiled EXE in hand, so one of the methods is to use reflection (reflection ). The following are some examples. For more information, see.

Assume that the third-party application EXE we want to reuse is composed of the followingCodeCompiled:

 Using System; Namespace Mynamespace { Public   Class MyApp { Public Mynestedobject = Null ; Public   Class Mynestedobject { Public   String Name ;} Public Enum fourseasonsenum {spring, summer, autumn, winter}Public MyApp (){} Public MyApp (mynamespace. myform form ){} Public Mynestedobject foo1 (fourseasonsenum season ){ Return   This . Mynestedobject ;} Public  String Foo2 (){ Return   "" ;} Static   Void Main (){}} Public   Class Myform {Public Myform (){}}}

The following describes how to use reflection to rewrite direct references:

1. generate an object using a constructor without Parameters

The code is

 
Mynamespace. MyApp APP =NewMynamespace. MyApp ();

This is the case if you use reflection to call it (remember using system. Reflection)

Assembly assem = assembly. LoadFile (@ "C: \ home \ workspace \ mynamespace \ myapp.exe"); Type myapptype = assem. GetType ("Mynamespace. MyApp"); Constructorinfo myapptype_constructor = myapptype. getconstructor (NewType [] {}); object APP = myapptype_constructor.invoke (NewObject [] {});

2. Use constructors with parameters to generate objects

The code is

Mynamespace. MyApp APP =NewMynamespace. MyApp (NewMynamespace. myform ());

If you use reflection to call it, you need to write it like this.

Assembly assem = assembly. LoadFile ( @ "C: \ home \ workspace \ mynamespace \ myapp.exe" ); Type myapptype = assem. GetType ( "Mynamespace. MyApp" ); Type myformtype = assem. 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. Call the object Method

The code is

Mynamespace. MyApp APP =NewMynamespace. MyApp ();StringSTR = app. foo2 ();

If you use reflection to call it, you need to write it like this.

 Assembly assem = assembly. loadFile ( @ "C: \ home \ workspace \ mynamespace \ myapp.exe "); Type myapptype = assem. 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 variables

The code is

 
Mynamespace. MyApp APP =NewMynamespace. MyApp (); mynamespace. MyApp. mynestedobject OBJ = app. mynestedobject; mynamespace. MyApp. mynestedobject obj2 =NewMynamespace. MyApp. mynestedobject (); app. mynestedobject = obj2;

Use reflection for calling. (Note that the mynestedobject class here is nested type. The name should be "mynamespace. MyApp + mynestedobject" instead of "mynamespace. MyApp. mynestedobject ")

Assembly assem = assembly. LoadFile ( @ "C: \ home \ workspace \ mynamespace \ myapp.exe" ); Type myapptype = assem. GetType ("Mynamespace. MyApp" ); Constructorinfo myapptype_constructor = myapptype. getconstructor ( New Type [] {}); object APP = myapptype_constructor.invoke ( New Object [] {}); Type mynestedobjecttype = assem. 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

The code is

 
Mynamespace. MyApp APP =NewMynamespace. MyApp (); mynamespace. MyApp. mynestedobject OBJ = app. foo1 (mynamespace. MyApp. fourseasonsenum. Spring );

If you use reflection to call it, you need to write it like this.

Assembly assem = assembly. LoadFile (@ "C: \ home \ workspace \ mynamespace \ myapp.exe" ); Type myapptype = assem. GetType ( "Mynamespace. MyApp" ); Constructorinfo myapptype_constructor = myapptype. getconstructor ( New Type [] {}); object APP = myapptype_constructor.invoke ( New Object [] {}); Type fourseasonsenumtype = assem. GetType ( "Mynamespace. MyApp + fourseasonsenum" ); Array fourseasonsenumvalues = enum. getvalues (fourseasonsenumtype); object springvalue = fourseasonsenumvalues. getvalue (0); object result = myapptype. getmethod ( "Foo1" ). Invoke (app, New Object [] {springvalue });

---

Finally, as long as possible, we should try not to use reflection, because the reflection performance is quite poor compared to direct calls.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.