根据属性名称更改对象属性的值

hyoung1484

我想根据属性的名称更改对象中目标属性的值。例子:

// Returns a list of all the properties of an object
PropertyInfo[] properties = GetProperties(myObject);

// Set the values of all properties that contain "Show_" in their name to -1
foreach (PropertyInfo property in properties)
{
    if (property.Name.Contains("Show_"))
    {
        // update myObject's property value to -1
        // i.e. something like:
        // myObject.(property.Name) = -1;
    }
}

我该怎么做呢?

谢谢

骆驼

您可以使用SetValue

property.SetValue(myObject, -1, null);

您可以在此处阅读文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章