Reputation: 1021
Suppose I have an empty interface class IBaseInterface which is used only to "label" implementing classes as being interfaces themselves.
Is there any way to do something like this?
For example:
public class MyClass : T where T : IBaseInterface
{
}
Upvotes: 0
Views: 646
Reputation: 244777
The type you're declaring isn't even generic. Something like this:
class MyClass<T> : T where T : IBaseInterface
could work under some circumstances (for example, if C++ templates were used instead of .Net generics), but it's simply not valid C# code.
I'm not sure what are the “labels” used for, but an interface with a property
ClassType ClassType { get; }
where
ClassType
is an enum
could work.
Upvotes: 0
Reputation: 391336
No, you can't do that, since the compiler has to know which interface the class implements when you declare the class. You can have generic parameters to the interface, but the actual interface has to be specified.
Upvotes: 2
Reputation: 1126
Not like that, there isn't. I would strongly recommend using a composition pattern to try and achieve whatever you're trying. As an alternative, you might find DynamicProxy (or some other proxy solution) is what you're going for.
Upvotes: 2