I want to do this:
[AttributeUsage(AttributeTargets.Property,
Inherited = false,
AllowMultiple = true)]
sealed class MyAttribute : Attribute
{
readonly string showName;
readonly Type controlType;
public Type ControlType
{
get { return controlType; }
}
readonly Func<Control, object> selector;
public Func<Control, object> Selector
{
get { return selector; }
}
public MyAttribute(string showName,
Type controlType,
Func<Control, object> selector)
{
this.showName = showName;
this.controlType = controlType;
this.selector = selector;
}
public string ShowName
{
get { return showName; }
}
}
class Foo
{
// problem. Do you have an idea?
[My("id number",
typeof(NumericUpDown),
Convert.ToInt32(control=>((NumericUpDown)control).Value))]
public int Id { get; set; }
}
I want to do attribute that contains name, type of control, and selector for take from the control the value of the property.
I try do it, and can’t.
No, you cannot use an anonymous method or lambda expression in an attribute decoration.
Incidentally, if you could, it would be (moving the
controldeclaration):But… You can’t. Either use the string name of a method that you resolve with reflection, or use something like a subclass of the attribute class with a virtual method override instead.