Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface that requires attribute to be implemented

Is there a way to create interface with attribute which has to be used by the class that implements interface?

I.e. if class Foo implements interface IFoo, and IFoo is defined to must have ObserveMeAttribute, then Foo must have ObserveMe defined on it in order to implement IFoo.

Edit:

Attribute is called EntityTypeAttribute and is used to specify entity type for the view model, with usage:

[EntityType(typeof(User))]
public class UserViewModel
{
...
}

Attribute is used in an extension method AddModel(T TModel) where attribute is read from TModel, and it's property Type entityType is used to create entity stub object.

like image 722
Admir Tuzović Avatar asked Nov 06 '12 17:11

Admir Tuzović


1 Answers

Simple answer is "no". If every object must have this value to implement IFoo why don't you make it part of the IFoo interface?

Attributes are primarily a mechanism for finding additional declarative information about a class/method/parameter at run-time by using reflection.

like image 128
Ian Mercer Avatar answered Oct 25 '22 11:10

Ian Mercer