Is there a way for an interface to be automatically generic for the implementer class?
For example, I want to create an interface that will return a delegate off of the implementing class. (Actually I want it to return an array of multiple functions, but I'm simplifying here.)
The following should work:
public interface IParentFinder<ChildClass>
{
Func<ChildClass, int> GetParentIdFunction();
}
And then to implement it, I have to write this:
public class AClass : IParentFinder<AClass>
{
int id;
int parentId;
static public Func<AClass, int> GetParentIdFunction()
{
return c => c.parentId;
}
}
However I only ever want this interface to be used where the generic template matches the implementing class. Is there some convenience that I can use so that instead I can just write this:
public class AClass : IParentFinder
{
...
}
I was hoping there would be some baseType
parameter I could use to specify the base type for generics. For example:
public interface IParentFinder
{
Func <baseType, int> GetParentIdFunction();
}
edited for clarity