mardi 30 juin 2015

Instantiate class with generic type list and determine the concrete type dynamically

I have a class with generic type list, but I need to instantiate the class and based on certain value determine the concrete type for the list.

Class looks as follows:

public class RestResponse<TData>
{
    public List<TData> Data { get; set; }
}

I tried following but I guess this is not the right approach:

var **genericRestTypeObj** = typeof(RestResponse<>);
switch(concreteTypeVal)
{ 
    case "1": {
        Type restObjType = typeof(apiModel);
        Type makeRestType = genericLogTypeObj.MakeGenericType(restObjType);
        var restObj = Activator.CreateInstance(makeRestType);
        **genericRestTypeObj** = restObj as GiTSLogApiOutputModel<AdminActionLogApiModel>;

        break;
    }
    case "2": {.....}
}

How can I do this?





Aucun commentaire:

Enregistrer un commentaire