【C#】(原创)C#反射知识分享之二

自小编来源于小编在就学JAVA的反射进度中,想打听一下C#的反光进程,所以写了须臾间学问总计,希望能为后来者的局地匡助

·         3 使用
MethodInfo
发现以下消息:方法的称谓、重临类型、参数、访问修饰符(如 public或 private)和兑现详细音信(如 abstract或 virtual)等。使用 Type 的 GetMethods 或
GetMethod 方法来调用特定的办法。

Example3 :

   Assembly assem1 = Assembly.Load("ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            Assembly assem = Assembly.LoadFrom("ClassLibrary1.dll");
            Console.Write(assem.FullName);
            AssemblyName assemName = assem.GetName();
            Console.WriteLine("\nName: {0}", assemName.Name);
            Console.WriteLine("Version: {0}.{1}",
                assemName.Version.Major, assemName.Version.Minor);
            int i,j;
            Type[] types = assem.GetTypes();
                     for (i = 0; i < types.GetLength(0); ++i)
            {
                Console.WriteLine(types[i].Name);
            }
          Type mytype = types[0];

      MethodInfo[] methods=mytype.GetMethods();
            for (i = 0; i < methods.Length; i++)
            {
                if (methods[i].MemberType == MemberTypes.Method)
                {
                    Console.WriteLine("---------------");
                    Console.WriteLine("method'name        : {0}", methods[i].Name);
                    Console.WriteLine("Public        : {0}", methods[i].IsPublic);
                    Console.WriteLine("Private        : {0}", methods[i].IsPrivate);
                    Console.WriteLine("Static        : {0}", methods[i].IsStatic);
                    Console.WriteLine("Abstract        : {0}", methods[i].IsAbstract);
                    Console.WriteLine("IsVirtual        : {0}", methods[i].IsVirtual);
                    Console.WriteLine("返回类型¨ª        : {0}", methods[i].ReturnType.ToString());
                    foreach (ParameterInfo pi in ((MethodInfo)methods[i]).GetParameters())
                    {
                        Console.WriteLine(" 参数 Parameter: Type={0}, Name={1}", pi.ParameterType, pi.Name);
                    }
                    Console.WriteLine("---------------");
                }
            }

  

打印结果如下:

………….


method’name        : run

C#,Public        : True

Private        : False

Static        : False

Abstract        : False

IsVirtual        : False

回去类型       : System.Void



method’name        : myReturnMethod

Public        : True

Private        : False

Static        : False

Abstract        : False

IsVirtual        : False

重临类型       : System.String

 参数 Parameter: Type=System.String, Name=abc

 参数 Parameter: Type=System.Int32, Name=i



method’name        : myReturnMethod

Public        : True

Private        : False

Static        : False

Abstract        : False

IsVirtual        : False

回去类型        : System.String

 参数 Parameter: Type=System.String, Name=abc

 参数 Parameter: Type=System.Int32, Name=i

 参数 Parameter: Type=System.String, Name=a


 

4.采取ConstructorInfo精晓构造函数的称呼、参数、访问修饰符(如pulic
或private)和落到实处详细新闻(如abstract或virtual)等。使用Type的GetConstructors或GetConstructor方法来调用特定的构造函数。 

Example4:

Assembly assem1 = Assembly.Load("ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
            Assembly assem = Assembly.LoadFrom("ClassLibrary1.dll");
            Console.Write(assem.FullName);
            AssemblyName assemName = assem.GetName();
            Console.WriteLine("\nName: {0}", assemName.Name);
            Console.WriteLine("Version: {0}.{1}",
                assemName.Version.Major, assemName.Version.Minor);
            int i,j;
            Type[] types = assem.GetTypes();

Type mytype = types[0];

ConstructorInfo constructorInfoObj = mytype.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null,
                CallingConventions.HasThis, types, null);
if(constructorInfoObj != null)
            {

                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("is not available.");
            }

  

相关文章