Aridane Álamo
Aridane Álamo

Reputation: 334

Accessing type argument on a generic class of generic type

Can't find a way and I am starting to think it's not possible but just in case I'll ask here.

Better explained with code..

I have this generic class definition, and I would like to be able to access the List's type argument so I can define a method in the GenericClassOfGenericType that accepts a string(taken from the type argument of the list)

//class definition
public class GenericClassOfGenericType<TGeneric>{ }

var instance = new GenericClassOfGenericType<List<string>>();

I tried something like;

public class GenericClassOfGenericType<TGeneric<TValue>> { 
 public void ProxyAdd(TValue value){ }
} 

But this doesn't work and can't find anything similar on google.

I can add a second type definition to the GenericClassOfGenericType and a validator in the constructor to be sure the generic list has this second type as type argument but it doesn't feel good at all.

[Edit] Also can't find a constraint that forces you to assign a generic type as an argument (makes sense as generics are also strong typed, but it makes me think that's not possible).

Upvotes: 1

Views: 97

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

How about that:

public class GenericClassOfGenericType<TGeneric, TValue>
    where TGeneric : IList<TValue> 

Upvotes: 2

Related Questions