(原创)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
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."); }