转:http://www.jb51.net/article/25863.htm 首先建立一个测试的类 复制代码代码如下: public class MyClass { public int one { set; get; } public int two { set; get; } public int five { set; get; } public int three { set; get; } public int four { set; get; } } 然后编写反射该类的代码 …
public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type = aa.GetType();//获取类型 System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); propertyInfo.SetValue(aa, , null);//给对应属性赋值 in…
除了 new 之外的创建对象的方法 通过 new 创建对象,会使得程序面向实现编程,先举个例子,某个果园里现在有两种水果,一种是苹果,一种是香蕉,有客户想采摘园子里的水果,要求用get()方法表示即可 一般情况下,最直接的写法为: public class Apple { public void get() { System.out.println("得到苹果"); } } public class Banana { public void get() { System.out.p…