反射常用的一些方法

来源:互联网 发布:淘宝卖家插件有哪些 编辑:程序博客网 时间:2024/05/10 00:32

 

首先我们建立一个类库,将它生成为HelloWorld.dll,

 

  1. using System;
  2.      namespace Webtest
  3.      {
  4.         public interface interface1
  5.          {
  6.               int add();
  7.          }
  8.          public class ReflectTest:interface1
  9.          {
  10.              public String Write;
  11.              private String Writec;
  12.              public String Writea
  13.              {
  14.                  get
  15.                  {
  16.                      return Write;
  17.                  }
  18.                  set
  19.                  {
  20.                      Write = value;
  21.                  }
  22.              }
  23.              private String Writeb
  24.              {
  25.                  get
  26.                  {
  27.                      return Writec;
  28.                  }
  29.                  set
  30.                  {
  31.                      Writec = value;
  32.                  }
  33.              }
  34.               public ReflectTest()
  35.               {
  36.                   this.Write = "Write";
  37.                   this.Writec = "Writec";
  38.               }
  39.              public ReflectTest(string str1,string str2)
  40.              {
  41.                  this.Write = str1;
  42.                  this.Writec = str2;
  43.          }
  44.              public string WriteString(string s,int b)
  45.              {
  46.                  return "欢迎您," + s + "---" + b; ;
  47.              }
  48.               public static string WriteName(string s)
  49.               {
  50.                  return "欢迎您光临," + s;
  51.               }
  52.              public string WriteNoPara()
  53.              {
  54.                 return "您使用的是无参数方法";
  55.              }
  56.              private string WritePrivate()
  57.              {
  58.                  return "私有类型的方法";
  59.              }
  60.              public int add()
  61.              {
  62.                  return 5;
  63.              }
  64.          }
  65.     }
  66. 然后,建立再建立一个项目引入该HelloWorld.dll

  1.     using System;
  2.     using System.Threading;
  3.     using System.Reflection;
  4.     class Test
  5.     {
  6.        delegate string TestDelegate(string value,int value1);
  7.        static void Main()
  8.         {
  9.             //Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相同的效果
  10.             Assembly t = Assembly.Load("HelloWorld");
  11.            foreach (Type aaa in t.GetTypes())
  12.            {
  13.                 //Console.Write(aaa.Name);   //显示该dll下所有的类
  14.             }
  15.             Module[] modules = t.GetModules();
  16.             foreach (Module module in modules)
  17.             {
  18.                 //显示模块的名字本例为"HelloWorld.dll"
  19.                 //Console.WriteLine("module name:" + module.Name);
  20.             }
  21.            
  22.            
  23.             //得到具体的类的类型,和下面一个效果
  24.             Type a = typeof(Webtest.ReflectTest);
  25.             //Type a = t.GetType("Webtest.ReflectTest");//
  26.             //Console.Write(a.Name);
  27.             string[] bb =...{ "aaaa""bbbbb" };
  28.            
  29.             //创建该类的实例,后面的bb为有参构造函数的参数
  30.             object obj = Activator.CreateInstance(a,bb); 
  31.             //object obj = t.CreateInstance("Webtest.ReflectTest");

  32.             MethodInfo[] miArr = a.GetMethods();
  33.             foreach (MethodInfo mi0 in miArr)
  34.            {
  35.                 //Console.Write(mi0.Name);  //显示所有的共有方法
  36.            }
  37.             MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法
  38.             object[] aa=...{"使用的是带有参数的非静态方法",2};
  39.             string s = (string)mi.Invoke(obj,aa); //带参数方法的调用
  40.             MethodInfo mi1 = a.GetMethod("WriteName");
  41.             String[] aa1 =...{"使用的是静态方法"};
  42.             string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用
  43.             MethodInfo mi2 = a.GetMethod("WriteNoPara");
  44.             string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法调用
  45.             MethodInfo mi3 = a.GetMethod("WritePrivate",BindingFlags.Instance | 
  46.                                           BindingFlags.NonPublic);
  47.             string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调

  48.             //Console.Write(s3);
  49.             PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance 
  50. | BindingFlags.NonPublic | BindingFlags.Public);
  51.             foreach (PropertyInfo pi in piArr)
  52.             {
  53.              //Console.Write(pi.Name);  //显示所有的方法
  54.             }
  55.             PropertyInfo pi1=a.GetProperty("Writea");
  56.             //pi1.SetValue(obj, "Writea", null);
  57.             //Console.Write(pi1.GetValue(obj,null));
  58.                     PropertyInfo pi2 = a.GetProperty("Writeb"
  59. BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
  60.             pi2.SetValue(obj, "Writeb"null);
  61.             //Console.Write(pi2.GetValue(obj, null));
  62.             FieldInfo fi1 = a.GetField("Write");
  63.             //Console.Write(fi1.GetValue(obj));
  64.             ConstructorInfo[] ci1 = a.GetConstructors();
  65.             foreach (ConstructorInfo ci in ci1)
  66.             {
  67.                 //Console.Write(ci.ToString()); //获得构造函数的形式
  68.             }
  69.             ConstructorInfo asCI = a.GetConstructor(new Type[] ...{ 
  70. typeof(string), typeof(string) });
  71.             //Console.Write(asCI.ToString());
  72.             Webtest.interface1 obj1 = 
  73. (Webtest.interface1)t.CreateInstance("Webtest.ReflectTest");
  74.             Webtest.ReflectTest obj2 = 
  75. (Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
  76.             //Console.Write(obj1.add());典型的工厂模式
  77.             foreach (Type tt in t.GetTypes())
  78.             {
  79.                 if (tt.GetInterface("interface1")!=null)
  80.                 {
  81.                     Webtest.interface1 obj3 = (Webtest.interface1)Activator.CreateInstance(a);
  82.                     //Console.Write(obj3.add());
  83.                 }
  84.             }
  85.             TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, 
  86. "WriteString");
  87.              //动态创建委托的简单例子
  88.             Console.Write(method("str1", 2));
  89.             Console.Read();
  90.         }
  91.     }

 


    在这里我把我们常用的方法,属性,等全部整理了出来,大家不要嫌弃乱,静下心来,自己按照我的分隔一部分一部分的来,保证你对反射的学习,会事半功倍.当然有关于其方法我会继续补充,想了这么些就先写下来吧.

 

原创粉丝点击