Reputation: 1935
public interface IMy<T>
{
T Implementer
{
get;
}
}
public class MyClass : IMy<MyClass>
{
...
}
But what I want is this:
public interface IMy
{
I Implementer
{
get;
}
}
Please accept that for some ca-razy reason I need an interface that defines a method that returns in the type of the implementer. No? Okay, suppose it's something like an XML explorer, so call my interface ITree
for example.
Interfaces primarily help me organize (separate), but when I have one that requires I rely on a convention, it seems to defeat the purpose.
Upvotes: 2
Views: 439
Reputation: 9587
You want to be able to say "The interface should know who implements it, and automatically provide the implementer's type in its definition, to be inherited by the implementer"? Like
public interface IMy
{
ImplicitImplementerType SomeProperty
{
get;
}
}
I'm pretty sure that is impossible. Such a thing may be achieved in more dynamic languages though (I'm guessing here).
Upvotes: 1
Reputation: 171188
You cannot statically specify such a return type. In C# (and in the CLR) there is no "thistype". But you can find it out at runtime:
var thisType = this.GetType()
Since I don't think this is what you need, I have to inform you that it is not possible to do what you want.
Of course, the first code snippet of yours would work but you don't want that.
Upvotes: 1