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

10年积累的网站设计制作、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有陕西免费网站建设让你可以放心的选择与我们合作。
- usingSystem;
 - namespaceWebtest
 - ...{
 - publicinterfaceinterface1
 - ...{
 - intadd();
 - }
 - publicclassReflectTest:interface1
 - ...{
 - publicStringWrite;
 - privateStringWritec;
 - publicStringWritea
 - ...{
 - get
 - ...{
 - returnWrite;
 - }
 - set
 - ...{
 - Write=value;
 - }
 - }
 - privateStringWriteb
 - ...{
 - get
 - ...{
 - returnWritec;
 - }
 - set
 - ...{
 - Writec=value;
 - }
 - }
 - publicReflectTest()
 - ...{
 - this.Write="Write";
 - this.Writec="Writec";
 - }
 - publicReflectTest(stringstr1,stringstr2)
 - ...{
 - this.Write=str1;
 - this.Writec=str2;
 - }
 - publicstringWriteString(strings,intb)
 - ...{
 - return"欢迎您,"+s+"---"+b;;
 - }
 - publicstaticstringWriteName(strings)
 - ...{
 - return"欢迎您光临,"+s;
 - }
 - publicstringWriteNoPara()
 - ...{
 - return"您使用的是无参数方法";
 - }
 - privatestringWritePrivate()
 - ...{
 - return"私有类型的方法";
 - }
 - publicintadd()
 - ...{
 - return5;
 - }
 - }
 - }
 
然后,建立再建立一个项目引入该HelloWorld.dll:
- usingSystem;
 - usingSystem.Threading;
 - usingSystem.Reflection;
 - classTest
 - ...{
 - delegatestringTestDelegate(stringvalue,intvalue1);
 - staticvoidMain()
 - ...{
 - //AssemblyAssemblyt=Assembly.LoadFrom("HelloWorld.dll");与下面相同的效果
 - AssemblyAssemblyt=Assembly.Load("HelloWorld");
 - foreach(Typeaaaint.GetTypes())
 - ...{
 - //Console.Write(aaa.Name);//显示该dll下所有的类
 - }
 - Module[]modules=t.GetModules();
 - foreach(Modulemoduleinmodules)
 - ...{
 - //Console.WriteLine("modulename:"+module.Name);//显示模块的名字本例为"HelloWorld.dll"
 - }
 - Typea=typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果
 - //Typea=t.GetType("Webtest.ReflectTest");//
 - //Console.Write(a.Name);
 - string[]bb=...{"aaaa","bbbbb"};
 - objectobj=Activator.CreateInstance(a,bb);//创建该类的实例,后面的bb为有参构造函数的参数
 - //objectobj=t.CreateInstance("Webtest.ReflectTest");//与上面方法相同
 - MethodInfo[]miArr=a.GetMethods();
 - foreach(MethodInfomi0inmiArr)
 - ...{
 - //Console.Write(mi0.Name);//显示所有的共有方法
 - }
 - MethodInfomi=a.GetMethod("WriteString");//显示具体的方法
 - object[]aa=...{"使用的是带有参数的非静态方法",2};
 - strings=(string)mi.Invoke(obj,aa);//带参数方法的调用
 - MethodInfomi1=a.GetMethod("WriteName");
 - String[]aa1=...{"使用的是静态方法"};
 - strings1=(string)mi1.Invoke(null,aa1);//静态方法的调用
 - MethodInfomi2=a.GetMethod("WriteNoPara");
 - strings2=(string)mi2.Invoke(obj,null);//不带参数的方法调用
 - MethodInfomi3=a.GetMethod("WritePrivate",BindingFlags.Instance|BindingFlags.NonPublic);
 - strings3=(string)mi3.Invoke(obj,null);//私有类型方法调用
 - //Console.Write(s3);
 - PropertyInfo[]piArr=a.GetProperties
 
(BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);- foreach(PropertyInfopiinpiArr)
 - ...{
 - //Console.Write(pi.Name);//显示所有的方法
 - }
 - PropertyInfopi1=a.GetProperty("Writea");
 - //pi1.SetValue(obj,"Writea",null);
 - //Console.Write(pi1.GetValue(obj,null));
 - PropertyInfopi2=a.GetProperty
 
("Writeb",BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);- pi2.SetValue(obj,"Writeb",null);
 - //Console.Write(pi2.GetValue(obj,null));
 - FieldInfofi1=a.GetField("Write");
 - //Console.Write(fi1.GetValue(obj));
 - ConstructorInfo[]ci1=a.GetConstructors();
 - foreach(ConstructorInfociinci1)
 - ...{
 - //Console.Write(ci.ToString());//获得构造函数的形式
 - }
 - ConstructorInfoasCI=a.GetConstructor(newType[]...{
 - typeof(string),typeof(string)});
 - //Console.Write(asCI.ToString());
 - Webtest.interface1obj1=(Webtest.interface1)t.CreateInstance
 
("Webtest.ReflectTest");- Webtest.ReflectTestobj2=(Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
 - //Console.Write(obj1.add());典型的工厂模式
 - foreach(Typettint.GetTypes())
 - ...{
 - if(tt.GetInterface("interface1")!=null)
 - ...{
 - Webtest.interface1obj3=(Webtest.interface1)Activator.CreateInstance(a);
 - //Console.Write(obj3.add());
 - }
 - }
 - TestDelegatemethod=(TestDelegate)Delegate.CreateDelegate
 
(typeof(TestDelegate),obj,"WriteString");- //动态创建委托的简单例子
 - Console.Write(method("str1",2));
 - Console.Read();
 - }
 - }
 
在这里我把我们常用的方法,属性,等全部整理了出来,大家不要嫌弃乱,静下心来,自己按照我的分隔一部分一部分的来,保证你对C#反射方法的学习,会事半功倍.当然有关于其方法我会继续补充,想了这么些就先写下来吧。
【编辑推荐】
                本文名称:C#反射方法学习总结
                
                链接地址:http://www.csdahua.cn/qtweb/news46/481346.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网