Generic Interface Casting

Hi All,

I've got a class that wraps other objects. In .net 1.1 I used an interface like:

public interface IMyWrapper{

object Data{get;}

}

In .net 2.0 I thought I would try something like:

public interface IMyWrapper<T>{

T Data{get;}

}

Now consider the use of this interface in a method such as:

public void MyMethod(object o){

//in 1.1

if (o is IMyWrapper){

//Do something else with the wrapped data

AnotherMethod( (o as IMyWrapper).Data)

}

//in 2.0

if ( o.GetType().GetGenericTypeDefinition() == typeof(IMyWrapper<>) ) {

//Do something with the wrapped data

AnotherMethod( (o as IMyWrapper<>).Data ) //Surprise, this doesn't work

}

If I don't know what the type of the generic interface is at run time how can I access the 'Data' member Here are my initial thoughts:

-Use reflection: while I'm sure this will work, it seems like quite a bit of work just to get at the interface member. In a nutshell, I think the mechinism would be to find the interface (i.e. IMyWrapper`1) then Invoke the get_Data property. Messy.

-Use a base class or typed interface (i.e. IMyTypedWrapper : IMyWrapper<MyClass>): again, while I'm sure this will work it seems like I am adding the benefits of generics to limit casting then removing the flexibility.

I feel like I'm missing the forest in all the trees! Any thoughts are aprreciated. Thanks!



Answer this question

Generic Interface Casting

  • klotz

    Why would you use a generic interface if you never need the typed value After all, what you'll be sending the method is an object (since you don't really know what the type parameter is).

    Maybe you should reconsider the whole deal. The only reason for you to create the interface is if you actually know the type parameter. If these are the only scenarios you need this for, there's no added value in the generic interface.

    On the other hand, if you insist on using this solution, I think your IMyWrapper<> should inherit from the non-generic IMyWrapper. This way, you could use IMyWrapper for functionality unsupported by the generic interface.

    Hope this helped.


  • Loranga

    It does help. Thanks. I actually started using the approach you suggested and for reasons I can't remember didn't follow through with it.

    As far as why I would want to do this, in my example you are correct, I don't need to know the type but there are other areas in the project where the type is known and the generic interface is useful. My example doesn't really have anything to do with how I leverage the generic interface only how I access a generic member in an area where I don't know the type. In my real world example, I want to set the SelectedObject of a grid view to my wrapped data (not the wrapper) and, at that point, I don't care what the type is (or at least I hadn't until now ).


  • bill777

    Great. I'm glad I was able to help.

    Please don't forget to mark my message as the answer to close the question :)


  • Generic Interface Casting