这篇文章主要讲解了“C#中反射方法的用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中反射方法的用法”吧!
首先我们建立一个类库,将它生成为HelloWorld.dll:
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#中反射方法的用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!