Answers for "c# set property by reflection"

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
-2

c# object set property

myObject.GetType().GetProperty(property).SetValue(myObject, "Bob", null);
Posted by: Guest on February-01-2021

C# Answers by Framework

Browse Popular Code Answers by Language