📜  C# 通过反射与属性设置属性值 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:08.189000             🧑  作者: Mango

代码示例1
PropertyInfo[] properties = typeof(MyClass).GetProperties();
foreach(PropertyInfo property in properties)
{
    StoredDataValueAttribute attribute =
        Attribute.GetCustomAttribute(property, typeof(StoredDataValueAttribute)) as StoredDataValueAttribute;

    if (attribute != null) // This property has a StoredDataValueAttribute
    {
         property.SetValue(instanceOfMyClass, attribute.DataValue, null); // null means no indexes
    }
}