How to Inherit attribute with interface ?

M

Microsoft

I have Interface defined with custom attribute. My class implements the
same interface , but I'm not able to see any attributes derived from
interface.( I did set the inherited to true) .
 
J

John Wood

Are you looking for attributes on the class, or on the interface?
Remember that classes don't "inherit" interfaces, they implement them. If
you want to get the attributes of an interface implemented by your class,
you'll get have to do a GetInterfaces on the class type, and then use that
type to get hold of your attribute.

John Wood
Blog: http://spaces.msn.com/members/johnwood
 
M

Microsoft

Hi John ,

I really appreciate your quick response.

As you point out my class implements interface

This is My interface declaration

public interface ICatalog
{
[LengthValidator("Basic Unit",FieldLength.Basic),
RequiredValidator("Basic Unit"),
UIAttribute(FieldLength.Basic, true),
EnumAttribute((int)CatalogProperties.BASIC)]
string Basic
{
get;set;
}
}


public class Catalog : ICatalog
{
private string _basic;
public string Basic
{
get
{
return this._basic;
}
set
{
this._basic = value.ToUpper().Trim();
}
}
}


This is how I'm trying to read custom attribute properties in my code where I use catalog

Catalog cat = new Catalog();

UIAttribute[] uiAttrib = null;
PropertyInfo[] itemTypeProperties= null;
itemTypeProperties = cat.GetType().GetProperties();
PropertyInfo propInfo;

for(int i= 0; i < itemTypeProperties.Length; i++ )
{
propInfo = itemTypeProperties;
uiAttrib = (UIAttribute[])propInfo.GetCustomAttributes(typeof(UIAttribute),true);
if(uiAttrib != null && uiAttrib.Length > 0)
{
int i = uiAttrib[0].MaxLength;
}
}


According to your suggestion I have to get class type and use GetInterfaces() to read all the properties definded in the interface , am I right?

If My Class end up implementing multiple interface , I have to get each interfaces and get propoerties and check the custom attribute ?

Since properties are implemented in Catalog class from ICatalog interface , why properites are not directly inheriting the custom attributes defined?

Thanks
Baski.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top