如何动态访问类的属性
如果动态创建了一个类.那么如何通过反射来遍历属性.
比如用,Invoker方法,来调用属性.关键是要用属性的名字.能不能不用属性的名字来自动暴露属性呢. --------------------编程问答--------------------
using System;
namespace Webtest
{
public inte易做图ce inte易做图ce1
{
int add();
}
public class ReflectTest:inte易做图ce1
{
public String Write;
private String Writec;
public String Writea
{
get
{
return Write;
}
set
{
Write = value;
}
}
private String Writeb
{
get
{
return Writec;
}
set
{
Writec = value;
}
}
public ReflectTest()
{
this.Write = "Write";
this.Writec = "Writec";
}
public ReflectTest(string str1,string str2)
{
this.Write = str1;
this.Writec = str2;
}
public string WriteString(string s,int b)
{
return "欢迎您," + s + "---" + b; ;
}
public static string WriteName(string s)
{
return "欢迎您光临," + s;
}
public string WriteNoPara()
{
return "您使用的是无参数方法";
}
private string WritePrivate()
{
return "私有类型的方法";
}
public int add()
{
return 5;
}
}
}然后,建立再建立一个项目引入该HelloWorld.dll,
using System;
using System.Threading;
using System.Reflection;
class Test
{
delegate string TestDelegate(string value,int value1);
static void Main()
{
//Assembly t = Assembly.LoadFrom("HelloWorld.dll"); 与下面相同的效果
Assembly t = Assembly.Load("HelloWorld");
foreach (Type aaa in t.GetTypes())
{
//Console.Write(aaa.Name); //显示该dll下所有的类
}
Module[] modules = t.GetModules();
foreach (Module module in modules)
{
//Console.WriteLine("module name:" + module.Name);//显示模块的名字本例为"HelloWorld.dll"
}
Type a = typeof(Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果
//Type a = t.GetType("Webtest.ReflectTest");//
//Console.Write(a.Name);
string[] bb ={ "aaaa", "bbbbb" };
object obj = Activator.CreateInstance(a,bb); //创建该类的实例,后面的bb为有参构造函数的参数
//object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同
MethodInfo[] miArr = a.GetMethods();
foreach (MethodInfo mi0 in miArr)
{
//Console.Write(mi0.Name); //显示所有的共有方法
}
MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法
object[] aa={"使用的是带有参数的非静态方法",2};
string s = (string)mi.Invoke(obj,aa); //带参数方法的调用
MethodInfo mi1 = a.GetMethod("WriteName");
String[] aa1 ={"使用的是静态方法"};
string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用
MethodInfo mi2 = a.GetMethod("WriteNoPara");
string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法调用
MethodInfo mi3 = a.GetMethod("WritePrivate",BindingFlags.Instance | BindingFlags.NonPublic);
string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调用
//Console.Write(s3);
PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (PropertyInfo pi in piArr)
{
//Console.Write(pi.Name); //显示所有的方法
}
PropertyInfo pi1=a.GetProperty("Writea");
//pi1.SetValue(obj, "Writea", null);
//Console.Write(pi1.GetValue(obj,null));
PropertyInfo pi2 = a.GetProperty("Writeb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
pi2.SetValue(obj, "Writeb", null);
//Console.Write(pi2.GetValue(obj, null));
FieldInfo fi1 = a.GetField("Write");
//Console.Write(fi1.GetValue(obj));
ConstructorInfo[] ci1 = a.GetConstructors();
foreach (ConstructorInfo ci in ci1)
{
//Console.Write(ci.ToString()); //获得构造函数的形式
}
ConstructorInfo asCI = a.GetConstructor(new Type[] { typeof(string), typeof(string) });
//Console.Write(asCI.ToString());
Webtest.inte易做图ce1 obj1 = (Webtest.inte易做图ce1)t.CreateInstance("Webtest.ReflectTest");
Webtest.ReflectTest obj2 = (Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
//Console.Write(obj1.add());典型的工厂模式
foreach (Type tt in t.GetTypes())
{
if (tt.GetInte易做图ce("inte易做图ce1")!=null)
{
Webtest.inte易做图ce1 obj3 = (Webtest.inte易做图ce1)Activator.CreateInstance(a);
//Console.Write(obj3.add());
}
}
TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString");
//动态创建委托的简单例子
Console.Write(method("str1", 2));
Console.Read();
}
}
供你参考
补充:.NET技术 , C#