Doesn't .net attribute support delegation?

57 views Asked by At

I'm trying to use delegation when pass param to attribute,but compiler got a error:"The constructor parameter condition is not a valid characteristic parameter types".Doesn't .net attribute support delegation?Or is there a alternative to pass a Func to attribute? code:

public class Class1
{
    [ValidateIf(c=>c=="hellow")]
    public string testStr { get; set; }
}
public class ValidateIfAttribute : ValidationAttribute
{
    public Func<object, bool> Condition { get; set; }
    public ValidateIfAttribute(Func<object, bool> condition) : base()
    {
        this.Condition = condition;
    }
    public override bool IsValid(object value)
    {
        if (Condition(value))
        {
            return true;
        }
        return base.IsValid(value);
    }
}
1

There are 1 answers

1
Emily On

Try using delegates? or are you trying to achieve something special in this case?

https://msdn.microsoft.com/en-us/library/ms173171.aspx