问题描述
如果动态创建了一个类.那么如何通过反射来遍历属性.比如用,Invoker方法,来调用属性.关键是要用属性的名字.能不能不用属性的名字来自动暴露属性呢.
解决方案
解决方案二:
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(){//Assemblyt=Assembly.LoadFrom("HelloWorld.dll");与下面相同的效果Assemblyt=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();}}
供你参考