dimanche 9 août 2015

IS it possible to create an object of a generic class with dynamic type arguments? [duplicate]

This question already has an answer here:

Say, we have a generic class like this:

public sealed class Class1<T>
{
    private T _value;
    public T Value
    {
        get { return _value; }
    }
}

I want to initiate an object of type Class1 but type argument T is only dynamically available using GetType(). Is that possible to use a generic class with dynamic type as follows?

Class1<anObject.GetType()> obj = new Class1<anObject.GetType()>();

I know this code even can not be compiled but I need some sort of generic usage like this.





Aucun commentaire:

Enregistrer un commentaire