Try this may require a little tweaking but should be close:
using System;
using System.Reflection;
// A custom attribute to allow a target to have a display value.
public class DisplayName: Attribute {
// The constructor is called when the attribute is set.
public AnimalTypeAttribute(String Value) {
_value = Value;
}
// Keep a variable internally ...
protected string _value;
// .. and show a copy to the outside world.
public string Value {
get { return _value; }
set { _value = value; }
}
}
// A test class where property has its own display value.
class DisplayNameTestClass {
[DisplayName("Number of cars")]
public int Count
{
get { . . . }
set { . . . }
}
}
class DemoClass {
static void Main(string[] args) {
DisplayNameTestClass testClass = new DisplayNameTestClass
Type type = testClass.GetType();
FieldInfo myFieldInfo = type.GetField("Count")
DisplayName att =
(DisplayName)Attribute.GetCustomAttribute(myFieldI nfo,Type.GetType("DisplayName"));
att.Value = "Number of bikes";
}
}
"Kimmo Laine" wrote:
Hi is there a way to change propertys attribute from the code?
LetÂīs say that i have the following property in my class:
[DisplayName("Number of cars")]
public int Count[
get { . . . }
set { . . . ]
}
Is there a way to change the displayname, from my code, at runtime to
"Number of bikes".
thx!
Kimmo Laine