C#反射方法学习总结

开发 后端
本文介绍今天把C#反射方法的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西。

在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,今天把C#反射方法的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西,在学好了这一切的基础上,大家可以学习C#反射方法的具体插件等应用,老鸟就不用看了。首先我们建立一个类库,将它生成为HelloWorld.dll:

  1. usingSystem;  
  2.  
  3. namespaceWebtest  
  4. ...{  
  5.  
  6. publicinterfaceinterface1  
  7. ...{  
  8. intadd();  
  9.  
  10. }  
  11. publicclassReflectTest:interface1  
  12. ...{  
  13.  
  14. publicStringWrite;  
  15. privateStringWritec;  
  16.  
  17. publicStringWritea  
  18. ...{  
  19. get  
  20. ...{  
  21. returnWrite;  
  22. }  
  23. set  
  24. ...{  
  25. Write=value;  
  26. }  
  27.  
  28. }  
  29.  
  30. privateStringWriteb  
  31. ...{  
  32. get  
  33. ...{  
  34. returnWritec;  
  35. }  
  36. set  
  37. ...{  
  38. Writec=value;  
  39. }  
  40.  
  41. }  
  42.  
  43. publicReflectTest()  
  44. ...{  
  45. this.Write="Write";  
  46. this.Writec="Writec";  
  47. }  
  48.  
  49. publicReflectTest(stringstr1,stringstr2)  
  50. ...{  
  51. this.Write=str1;  
  52. this.Writec=str2;  
  53.  
  54. }  
  55.  
  56. publicstringWriteString(strings,intb)  
  57. ...{  
  58. return"欢迎您,"+s+"---"+b;;  
  59. }  
  60.  
  61. publicstaticstringWriteName(strings)  
  62. ...{  
  63. return"欢迎您光临,"+s;  
  64. }  
  65.  
  66. publicstringWriteNoPara()  
  67. ...{  
  68. return"您使用的是无参数方法";  
  69. }  
  70.  
  71. privatestringWritePrivate()  
  72. ...{  
  73. return"私有类型的方法";  
  74. }  
  75.  
  76.  
  77. publicintadd()  
  78. ...{  
  79. return5;  
  80. }  
  81. }  

然后,建立再建立一个项目引入该HelloWorld.dll:

  1. usingSystem;  
  2.  
  3. usingSystem.Threading;  
  4. usingSystem.Reflection;  
  5.  
  6.  
  7. classTest  
  8. ...{  
  9. delegatestringTestDelegate(stringvalue,intvalue1);  
  10.  
  11. staticvoidMain()  
  12. ...{  
  13. //AssemblyAssemblyt=Assembly.LoadFrom("HelloWorld.dll");与下面相同的效果  
  14. AssemblyAssemblyt=Assembly.Load("HelloWorld");  
  15.  
  16.  
  17. foreach(Typeaaaint.GetTypes())  
  18. ...{  
  19. //Console.Write(aaa.Name);//显示该dll下所有的类  
  20. }  
  21.  
  22.  
  23. Module[]modules=t.GetModules();  
  24.  
  25. foreach(Modulemoduleinmodules)  
  26. ...{  
  27. //Console.WriteLine("modulename:"+module.Name);//显示模块的名字本例为"HelloWorld.dll"  
  28. }  
  29.  
  30.  
  31. Typea=typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果  
  32.  
  33. //Typea=t.GetType("Webtest.ReflectTest");//  
  34. //Console.Write(a.Name);  
  35.  
  36.  
  37. string[]bb=...{"aaaa","bbbbb"};  
  38. objectobj=Activator.CreateInstance(a,bb);//创建该类的实例,后面的bb为有参构造函数的参数  
  39. //objectobj=t.CreateInstance("Webtest.ReflectTest");//与上面方法相同  
  40.  
  41.  
  42. MethodInfo[]miArr=a.GetMethods();  
  43. foreach(MethodInfomi0inmiArr)  
  44. ...{  
  45. //Console.Write(mi0.Name);//显示所有的共有方法  
  46. }  
  47.  
  48.  
  49. MethodInfomi=a.GetMethod("WriteString");//显示具体的方法  
  50. object[]aa=...{"使用的是带有参数的非静态方法",2};  
  51. strings=(string)mi.Invoke(obj,aa);//带参数方法的调用  
  52.  
  53. MethodInfomi1=a.GetMethod("WriteName");  
  54. String[]aa1=...{"使用的是静态方法"};  
  55. strings1=(string)mi1.Invoke(null,aa1);//静态方法的调用  
  56.  
  57. MethodInfomi2=a.GetMethod("WriteNoPara");  
  58. strings2=(string)mi2.Invoke(obj,null);//不带参数的方法调用  
  59.  
  60. MethodInfomi3=a.GetMethod("WritePrivate",BindingFlags.Instance|BindingFlags.NonPublic);  
  61. strings3=(string)mi3.Invoke(obj,null);//私有类型方法调用  
  62.  
  63. //Console.Write(s3);  
  64.  
  65.  
  66. PropertyInfo[]piArr=a.GetProperties
    (BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);  
  67. foreach(PropertyInfopiinpiArr)  
  68. ...{  
  69. //Console.Write(pi.Name);//显示所有的方法  
  70. }  
  71.  
  72.  
  73. PropertyInfopi1=a.GetProperty("Writea");  
  74. //pi1.SetValue(obj,"Writea",null);  
  75. //Console.Write(pi1.GetValue(obj,null));  
  76.  
  77. PropertyInfopi2=a.GetProperty
    ("Writeb",BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);  
  78. pi2.SetValue(obj,"Writeb",null);  
  79. //Console.Write(pi2.GetValue(obj,null));  
  80.  
  81. FieldInfofi1=a.GetField("Write");  
  82. //Console.Write(fi1.GetValue(obj));  
  83.  
  84.  
  85. ConstructorInfo[]ci1=a.GetConstructors();  
  86. foreach(ConstructorInfociinci1)  
  87. ...{  
  88. //Console.Write(ci.ToString());//获得构造函数的形式  
  89. }  
  90.  
  91. ConstructorInfoasCI=a.GetConstructor(newType[]...{
  92. typeof(string),typeof(string)});  
  93. //Console.Write(asCI.ToString());  
  94.  
  95.  
  96. Webtest.interface1obj1=(Webtest.interface1)t.CreateInstance
    ("Webtest.ReflectTest");  
  97. Webtest.ReflectTestobj2=(Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");  
  98. //Console.Write(obj1.add());典型的工厂模式  
  99.  
  100.  
  101. foreach(Typettint.GetTypes())  
  102. ...{  
  103. if(tt.GetInterface("interface1")!=null)  
  104. ...{  
  105. Webtest.interface1obj3=(Webtest.interface1)Activator.CreateInstance(a);  
  106. //Console.Write(obj3.add());  
  107. }  
  108. }  
  109.  
  110.  
  111. TestDelegatemethod=(TestDelegate)Delegate.CreateDelegate
    (typeof(TestDelegate),obj,"WriteString");  
  112. //动态创建委托的简单例子  
  113. Console.Write(method("str1",2));  
  114. Console.Read();  
  115. }  

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

【编辑推荐】

  1. 定义C#接口学习经验
  2. C# ListBox学习笔记
  3. 操作C# Dataset介绍
  4. C# ODBC访问MySQL数据库
  5. 浅析C#和Java不同点
责任编辑:佚名 来源: IT168
相关推荐

2009-08-21 18:01:32

C#匿名方法

2009-08-31 16:51:11

C# Main()方法

2009-08-27 09:27:49

C#扩展方法

2009-08-14 17:38:08

C#改写方法

2009-08-21 08:41:44

C#反射

2009-08-14 09:41:03

C#遗传算法

2009-08-13 18:21:52

C#学习笔记

2021-03-15 08:18:23

C#反射模块

2009-08-31 09:41:05

C#反射静态方法开发

2009-09-09 13:31:15

C# TextBox

2009-08-13 18:13:27

C#学习经验

2009-08-20 18:06:18

学习C#接口

2009-08-24 16:19:42

C# 泛型方法

2009-08-13 10:52:03

C#基础概念

2009-08-11 14:20:41

C# .NET学习经验

2009-04-10 09:55:44

C#反射.NET

2015-07-07 10:58:29

Swift语法高级

2015-07-07 10:43:59

Swift语法基础

2009-08-14 17:52:27

C#对象初始化

2009-09-01 13:10:39

C#读取Word
点赞
收藏

51CTO技术栈公众号