views:

69

answers:

4

Hi all,

I am looking to implement an interface which has a function which will return the type of the base class without using a generic interface. Is this possible?

class MyClass : MyInterface<MyClass> // Would rather use one below

class MyClass : MyInterface   // Functions already know to use MyClass
+2  A: 

You would have to do something like this:

interface MyInterface
{
    Type GetBaseType();
}

But at that point it would be simpler to call instance.GetType() since that is what the implementation of this method would most likely look like.

If by "type" you don't mean the reflected type but rather that you wish to be able to use the type from the interface statically at compilation time then you will need to make the interface generic.

Andrew Hare
+3  A: 

If I understand your question correctly, your only choice without generics is to use reflection to do this.

Gregory Higley
+1  A: 

Either

  1. You hard code the return type as MyClass

  2. You define the return type as MyInterface and then in the implementation of MyClass you can return a MyClass. The caller won't know what it's getting, but could use typeof to find out.

Lou Franco
+1  A: 

You achieve it like this:

interface IUserService : IService<User>

then your class would be:

class UserService : IUserService
Chris Missal