C # Gets the number of methods and parameters in the class by reflection, and the reflection calls the method with parameters

Source: Internet
Author: User
Using System;

Using System.Reflection; Namespace CONSOLEAPP2 {class Program {static void Main (string[] args) {//Reflection get Namespace +
            Class name String className = "Consoleapp2.classsample";
            String methodname = "Test1";
            Pass parameter object[] paras = new object[] {"My", "Computer"};
            var t = Type.GetType (className);

            Object obj = activator.createinstance (t); try {#region method one//Direct call MethodInfo methods = T.getmethod ("Test
                2 "); Method.
                Invoke (obj, paras);
                #endregion #region Method Two methodinfo[] info = t.getmethods (); for (int i = 0; i < info. Length;
                    i++) {var MD = info[i]; Method Name String mothodname = Md.
                    Name; Parameter set parameterinfo[] Paraminfos = Md. GeTparameters (); The method name is the same and has the same number of arguments if (mothodname = methodname && paraminfos.length = = paras. Length) {Md.
                    Invoke (obj, paras);
                #endregion} catch (Exception ex) {
            Throw ex;
        } console.readkey (); } class Classsample {public void test1 (string para1) {Console.WriteLine ("Mode 1
        {0}________test111 ", para1); } public void Test1 (string para1, String para2) {Console.WriteLine ("Mode 2 {0}________test111__
        ______{1} ", Para1, PARA2); } public void Test2 (string para1, String para2) {Console.WriteLine ("mode 3 {0}________test222__
        ______{1} ", Para1, PARA2); }
    }
}

In C #, to use reflection, we first need to understand the relationships of several classes in the following namespaces:

System.Reflection namespaces

(1) AppDomain: application domain, which can be understood as a logical container for a set of assemblies

(2) Assembly: assembly class

(3) Module: Modular class

(4) Type: The most core class that uses reflection to get type information

They are a subordinate relationship, that is to say, a AppDomain can contain N assembly, a assembly can contain n a module, and a module can contain n type.

In the program, if we want to dynamically load an assembly there are the following ways to use, namely, load, LoadFrom, LoadFile, LoadWithPartialName method.

The difference between the following assembly.loadfile and Assembly.LoadFrom is emphatically explained

1, assembly.loadfile only load the corresponding DLL file, such as Assembly.loadfile ("Abc.dll"), then load Abc.dll, if Abc.dll quoted Def.dll, Def.dll will not be loaded.

Assembly.LoadFrom is not the same, it loads the DLL file and other DLLs it references, such as the example above, and the Def.dll is also loaded.

2, with Assembly.LoadFrom load a assembly, will first check whether the previous has been loaded with the same name of the assembly, such as Abc.dll has two versions (version 1 in the directory 1, version 2 in the Directory 2), the program was initially loaded in version 1, When you load version 2 o'clock with Assembly.LoadFrom ("2\\abc.dll"), you cannot load it, but instead return to version 1. Assembly.loadfile will not do such a check, such as the above example replaced by Assembly.loadfile, you can load version 2 correctly.

LoadFile: Loads the contents of the assembly file on the specified path. LoadFrom: Loads the contents of the assembly file based on the file name of the assembly.

Finally, the calling method

Assembly outerasm = Assembly.LoadFrom (@ "Urpath\mydll.dll");

Calling methods in a DLL class

Type type = Outerasm. GetType ("Mydll.myclass");//Call type
MethodInfo method = type. GetMethod ("myvoid");//Calling method
If you need to pass the parameter

object[] paramertors = new object[] {"3087", "2005"};//parameter set
Object Test = method. Invoke (null, paramertors);//invoke Call method

Calling a form in a DLL

type outerform = Outerasm.gettype ("MyForm", false);//Find the specified window (Activator.CreateInstance ( Outerform) as Form). Show ()//Convert to form class, display 
Related Article

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.