Use reflection to call the classes and methods in other people's. net programs

Source: Internet
Author: User

If you want to reuse A. Net program written by someone else, but you only have one compiled EXE, 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 to be reused 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 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=new MyNamespace.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(new Type[]{});object app=MyAppType_Constructor.Invoke(new object[]{});

2. Use constructors with parameters to generate objects

The code is

MyNamespace.MyApp app=new MyNamespace.MyApp(new MyNamespace.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=new MyNamespace.MyApp();string str=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=new MyNamespace.MyApp();MyNamespace.MyApp.MyNestedObject obj=app.myNestedObject;MyNamespace.MyApp.MyNestedObject obj2=new MyNamespace.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=new MyNamespace.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.