views:

61

answers:

1

Why annotations doesnt work in interfaces ?

like:

public interface  IUser
{
    [Required]
    string FirstName { get; set; }
}

now if i made a class to implement that

 public partial class Customer:IUser
{
    public Customer()
    {
    }

    public string FirstName
    {
        get; set;
    }
}

it wouldnt enforce validation unless i mark property in the class too ! so what is the point of annotate it in the interface from the first place ! ! so any idea ?

+4  A: 

Well, the simple answer is that there is no point in annotating in the interface. As you've noted, calling Attribute.GetAttribute() (even if inherit is true) on a property does not return attributes decorated on the interface properties that your class has implemented. Presumably supporting such a facility would result in the potential for startling ambiguity when multiple interfaces are satisfied by the same implementation.

Kirk Woll