locked
How to specify <T> (type of elements) in the system.Collections.Generic.List<T> dynamically RRS feed

  • Question

  • User-1441714875 posted

    Hi All,

    How can I specify the  <T> (type of elements) in the system.Collections.Generic.List<T> dynamically.  I want to achieve some thing like below code 

     public void CreateList(object classObject, string className)
            {
                System.Collections.Generic.List<className> lstObject = new System.Collections.Generic.List<className>();
                lstObject.Add(classObject);
            }

    here I will get the <T> as a string parameter (className).  In this parameter I am passing the name of the class. For e.g. Customer. By using this parameter I want to create the List. ( System.Collections.Generic.List<className> lstObject = new System.Collections.Generic.List<className>();) 

    How I can achieve this.

    Note : Here the error what I am getting is : The type or namespace name 'className' could not be found. (are you missing a using directive or an assembly reference)

    Please help me on this

    Thanks and Regards,

    Shiyas

     

     

     

    Wednesday, July 17, 2013 2:48 AM

Answers

  • User-1910946339 posted
    public IList<T> CreateList<T>(T classObject)
    {
        var listObject = new List<T>();
        listObject.Add(classObject);
        return listObject;
    }

    The code you posted (even if it compiled and did what you want it to do) does nothing.  You attempt to create a list, add an item to it and then the list goes out of scope.  Net result -> nothing.

    My method returns the List that is created.  To call it you could write

    var newList = CreateList(someObject);


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2013 3:35 AM

All replies

  • User1083584480 posted

    in generic collection you need to define specific Type during object creation.

    you need change your code as below

     public void CreateList(object classObject, string className)
            {

    if (className=="Class1")

    {

               System.Collections.Generic.List<Class1> lstObject = new System.Collections.Generic.List<Class1>();

      lstObject.Add(classObject);

    }

    elseif (className=="Class2")

    {

               System.Collections.Generic.List<Class2> lstObject = new System.Collections.Generic.List<Class2>();

      lstObject.Add(classObject);

    }
     
             
            }

    Wednesday, July 17, 2013 2:58 AM
  • User-1441714875 posted

    Hi Sumit,

    Thank you for the reply.

    I don't want to use the If functionality (That is why I mentioned 'Dynamically' ). Is it possible to do without if or switch.

    Thanks and Regards,

    Shiyas

    Wednesday, July 17, 2013 3:07 AM
  • User1083584480 posted

    Then you should use reflection to perform this task.

    http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx

    var d1 = Type.GetType("GenericTest.TaskA`1"); // GenericTest was my namespace, add yours 
    Type[] typeArgs = { typeof(Item) };
    var makeme = d1.MakeGenericType(typeArgs);
    object
    o = Activator.CreateInstance(makeme);
    Wednesday, July 17, 2013 3:14 AM
  • User-1910946339 posted
    public IList<T> CreateList<T>(T classObject)
    {
        var listObject = new List<T>();
        listObject.Add(classObject);
        return listObject;
    }

    The code you posted (even if it compiled and did what you want it to do) does nothing.  You attempt to create a list, add an item to it and then the list goes out of scope.  Net result -> nothing.

    My method returns the List that is created.  To call it you could write

    var newList = CreateList(someObject);


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 17, 2013 3:35 AM
  • User-1441714875 posted

    Hi Paul,

    Thank you for the reply. This is what I expected. Thanks...

    Regards,

    Shiyas

    Wednesday, July 17, 2013 9:22 AM