zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    关于<form>标签
    javaEE学习随笔
    类与接口
    java学习中的一些总结
    java 对象的创建
    jQuery选择器
    CSS学习随笔
    JDBC笔记 二
    Java EE笔记 (1) JDBC
    泛型笔记
  • 原文地址:https://www.cnblogs.com/Sandheart/p/2998181.html
Copyright © 2011-2022 走看看