【发布时间】:2023-03-08 06:11:01
【问题描述】:
我想根据属性名称更改对象中目标属性的值。 示例:
// 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;
}
}
我该怎么做呢?
谢谢
【问题讨论】:
-
Tagging help: “你应该在标题中使用标签的唯一情况是它们与标题的对话语气有机。” - 我已经从您的问题标题中删除了“(C#)”标签。
-
property.SetValue(myObject, -1)
标签: c# reflection properties variable-assignment variable-names