神州教育培训网 打造高端培训课程,传播精品教育理念! 神州教育 神州培训 教育 企业培训 企业内训 企业管理 研修课程 简章 资本运作 房地产 国学 名师推荐

c# 如何通过反射 获取\设置属性值、


//定义类
public class MyClass
{
public int Property1 { get; set; }
}
static void Main()
{
MyClass tmp_Class = new MyClass();
tmp_Class.Property1 = 2;
Type type = tmp_Class.GetType(); //获取类型
System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); //获取指定名称的属性
int value_Old = (int)propertyInfo.GetValue(tmp_Class, null); //获取属性值
Console.WriteLine(value_Old);
propertyInfo.SetValue(tmp_Class, 5, null); //给对应属性赋值
int value_New = (int)propertyInfo.GetValue(tmp_Class, null);
Console.WriteLine(value_New);

}

 

其它应用请参考:http://www.bitscn.com/pdb/dotnet/200804/138760.html

 

posted on 2013-04-03 16:29  努力&快乐  阅读(74797)  评论(1编辑  收藏  举报

导航

神州教育培训网 打造高端培训课程,传播精品教育理念! 神州教育 神州培训 教育 企业培训 企业内训 企业管理 研修课程 简章 资本运作 房地产 国学 名师推荐