Answers for "C# setting property values through reflection with attributes"

C#
0

C# setting property values through reflection with attributes

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
    }
}
Posted by: Guest on July-12-2021

Code answers related to "C# setting property values through reflection with attributes"

C# Answers by Framework

Browse Popular Code Answers by Language