我需要在運行時更改屬性的參數。我簡化了我的問題到簡單的例子。在運行時更改自定義屬性的參數
屬性類:
[AttributeUsage(AttributeTargets.Property)]
public class MyAttribute : Attribute
{
public string Name { get; set; }
}
簡單的實體至極擁有裝飾性與屬性:
public class MyEntity
{
[MyAttribute(Name="OldValue1")]
public string Data1{ get; set; }
[MyAttribute(Name = "OldValue2")]
public string Data2 { get; set; }
}
我創建的類myEntity所的instace。我可以改變對象屬性的值,但是我不能改變對象實體的屬性名稱的值。可能嗎?
值對對象實體屬性我可以用這部分代碼更改:
entityProp.SetValue(entity,"NewData",null);
但我不改變屬性的屬性名稱如何價值的對象實體
這不行:
attProp.SetValue(attribute,"NewData",null);
值屬性格式名稱的仍然是原來的。
這裏是所有測試代碼。謝謝你,hellp。
[TestMethod]
public void Test()
{
var entity = new MyEntity
{
Data1 = "OldData",
Data2 = "OldData"
};
PropertyInfo[] entityProps = entity.GetType().GetProperties();
foreach (var entityProp in entityProps)
{
var attribute = Attribute.GetCustomAttribute(entityProp, typeof (MyAttribute)) as MyAttribute;
if (attribute != null)
{
//get attribute’s property NAME
PropertyInfo attProp= attribute.GetType().GetProperty("Name");
//get entity property value
var propertyValue = entityProp.GetValue(entity, null);
//get attribute’s property NAME value
var atributeNameValue = attProp.GetValue(entity, null);
TestContext.WriteLine(string.Format("property name:{0} property value: {1} : atribute name value: {2}\n",
entityProp.Name, propertyValue, atributeNameValue));
//change values
entityProp.SetValue(entity,"NewData",null);
//how can I change value of property Name on object entity ?
attProp.SetValue(attribute,"NewData",null);
}
}
TestContext.WriteLine(string.Format("After change\n"));
foreach (var entityProp in entityProps)
{
var attribute = Attribute.GetCustomAttribute(entityProp, typeof(MyAttribute)) as MyAttribute;
if (attribute != null)
{
PropertyInfo attProp = attribute.GetType().GetProperty("Name");
var propertyValue = entityProp.GetValue(entity, null);
var atributeNameValue = attProp.GetValue(entity, null);
TestContext.WriteLine(string.Format("property name:{0} property value: {1} : atribute name value: {2}\n",
entityProp.Name, propertyValue, atributeNameValue));
}
}
}
編輯:我刪除原帖,並添加非常簡單明確的樣本。對不起
附註:爲什麼你的第二個方法捕獲然後拋出異常? – 2012-04-06 18:03:59