Skip to content

Type related

Paresh Bhurke

Asked Sep 17, 2007, 9:14 PM EDT1 answer
Share
LinkedInX

Hi there,

I want to achieve something like this. Please let me know if it is possible or not.

Java
string strType = "System.String";
//I want to create List of actual type mentioned in the strType value.
List<strType> obj = new List<strType>();

Awaiting your reponse.

Paresh

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Got the solution.. thanks..

    Java
    string typeName = "System.String";
    Type listType = typeof(List<>);
    Type listElementType = Type.GetType(typeName);
    Type genericType = listType.MakeGenericType(listElementType);
    IList lobjlist = (IList)Activator.CreateInstance(genericType);
    Console.WriteLine(lobjlist.Count.ToString());
    lobjlist.Add("Paresh");
    Console.WriteLine(lobjlist.Count.ToString());
    Console.ReadKey();
    0