我试图从字典中更改控件属性,所以基本上字典中的键是该控件的属性名称,该值将是属性值。无论如何要做到这一点?从字典中分配.net控件属性
例如在我的字典中我有“名称”作为键和“buttonSave”作为值,我如何将它们与我的控件相关联以根据键和值设置其属性?
在此先感谢。
我试图从字典中更改控件属性,所以基本上字典中的键是该控件的属性名称,该值将是属性值。无论如何要做到这一点?从字典中分配.net控件属性
例如在我的字典中我有“名称”作为键和“buttonSave”作为值,我如何将它们与我的控件相关联以根据键和值设置其属性?
在此先感谢。
例子你如何在你的情况下,使用反射与方法PropertyInfo.SetValue
public class Customer
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
}
var dictionary = new Dictionary<string, object>
{
{"Id", new Guid()},
{"Name", "Phil"},
{"Phone", "12345678"}
};
var customer = new Customer();
foreach (var pair in dictionary)
{
var propertyInfo = typeof(Customer).GetProperty(pair.Key);
propertyInfo.SetValue(customer, pair.Value, null);
}
这就是我正在寻找的,谢谢队友。 – 2012-08-07 07:10:44
不客气! – 2012-08-07 07:15:42
using System.Reflection;
查找在MSDN
myControl.GetProperty("Name").SetValue(myControl, "buttonSave", null);
这也将是首先要检查,该场所存在的好主意,并且它有一个setter。 有关反射的更多信息,请参见here。
Yup欢呼指出, – 2012-08-07 07:11:32
你有没有看过可能使用反射或switch语句? – 2012-08-07 04:28:10