Inheriting Attributes from an Interface

Published 22 February 07 05:15 PM | danieb

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]

public class SomeAttribute : System.Attribute { }

 

public interface ISomeInterface

{

[SomeAttribute]

string Property { get; set;}

 

[SomeAttribute]

string GetProperty();

}

 

public class SomeClass : ISomeInterface

{

public string Property

{

get { throw new NotImplementedException(); }

set { throw new NotImplementedException(); }

}

public string GetProperty() { throw new NotImplementedException(); }

}

 

[TestMethod()]

public void TestAttributeOn_ISomeInterface_PropertyInfo()

{

Type t = typeof(ISomeInterface);

PropertyInfo propInfo = t.GetProperty("Property");

 

Assert.IsNotNull(System.Attribute.GetCustomAttribute(propInfo, typeof(SomeAttribute)));

}

[TestMethod()]

public void TestAttributeOn_SomeClass_PropertyInfo()

{

Type t = typeof(SomeClass);

PropertyInfo propInfo = t.GetProperty("Property");

Assert.IsNotNull(System.Attribute.GetCustomAttribute(propInfo, typeof(SomeAttribute), true));

}

[TestMethod()]

public void TestAttributeOn_SomeClass_GetProperty()

{

Type t = typeof(SomeClass);

MethodInfo methodInfo = t.GetMethod("GetProperty");

Assert.IsNotNull(System.Attribute.GetCustomAttribute(methodInfo, typeof(SomeAttribute), true));

}

 

So the second and third test (TestAttributeOn_SomeClass_PropertyInfo, TestAttributeOn_SomeClass_GetProperty) fails.

So if I think of WCF we define an interface with attributes.

 

Example:

 

[ServiceContract]

public interface ICustomers

{

[OperationContract]

string GetCustomer();

}

 

So surely if WCF can inherit attributes we should also be able to do so too!

But wait a minute in WCF only the interface is exposed. Damn!

 

So what is the solution? Recursively check for attributes on inherited interfaces?!

 

 

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: