List<T> of radnom Type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brboLikus
    New Member
    • Mar 2010
    • 5

    #1

    List<T> of radnom Type

    Hello!

    Is it possible to create a List<T> class where T is a random type? This is what I would like to do (or something similar because this doesn't work):
    Code:
    Object object = (Object)(new SomeClass());
    List<object.GetType()> list = new List<object.GetType()>;
    After that, "list" should be a list that contained "SomeClass" classes.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Maybe you can do something through reflection?

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Is there any reason you can't just use List<object>?

      The items you put into it should know what type they are...

      Code:
                  List<object> objList = new List<object>();
                  objList.Add("this is a test");
                  objList.Add((int)21);
                  objList.Add((float)3.1415);
      
                  foreach (object o in objList)
                  {
                      Console.WriteLine("Object:");
                      Console.WriteLine("\tType = " + o.GetType());
                      Console.WriteLine("\tValue = " + o.ToString());
                      Console.WriteLine();
                  }
      Object:
      Type = System.String
      Value = this is a test

      Object:
      Type = System.Int32
      Value = 21

      Object:
      Type = System.Single
      Value = 3.1415

      Comment

      • brboLikus
        New Member
        • Mar 2010
        • 5

        #4
        Unfortunately, the reason I am looking for this is because I can't do that. :) The reason is that i need to XMLSerialize the list and if I put "object" the XMLSerializer reports that there is an unexpected class and that I should use XMLInclude for this. Now, the problem is I don't have access to the classes I need to serialize so I cannot add this to it's declaration. The only way I could think of that could solve this is if I made a collection of generic objects that have a property of the exact type I need to serialize. My problem looks something like this:

        Code:
        public class Info
        {
            Object _Object;
            String _Tag;
        
            public class Info() { }
        
            public class Info(Object Object, String Tag)
            {
                _Object = Object;
                _Tag = Tag;
            }
        
            Object Object
            {
                get { return _Object; }
                set { _Object = value; }
            }
        
            String Tag
            {
                get { return _Tag; }
                set { _Tag= value; }
            }
        }
        
        public class InfoHolder : List<Info>
        {
            public void Add(Object Object, String Tag)
            {
                this.Add(new Info(Object, Tag));
            }
        }
        Now I have to serialize the InfoHolder class but this throws an exception if Object property of an Info class is a statistically uncommon class (in other words - not int, double, bool and such).

        Now, what I was thinking is that I could define a new class InfoType class like this:

        Code:
        public class InfoType<InType> : Info
        {
            public class InfoType() { }
        
            public class Info(InType Object, String Tag)
            {
                _Object = (InType)Object;
                _Tag = Tag;
            }
        
            InType Object
            {
                get { return (InType)_Object; }
                set { _Object = value; }
            }
        }
        And then I could populate the InfoHolder with this derived class, but the problem is I determine the type of the object dynamically and this is the main issue - I can't initialize the InfoType<?> class because I have only the Type class of the object that I got using Object.getType( ).

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You might like this:
          omegacoder.com uses omega symbol representing top level matching coder technical word, highlighting high-end core development positioning. It fits enterprises engaged in high-performance system research and development to build global official sites with strong advanced tech sense and support for safe transfer.


          [code=C#]

          Type d1 = typeof(List<>);
          Type[] typeArgs = { typeof(string) };
          Type makeme = d1.MakeGenericT ype(typeArgs);
          object o = Activator.Creat eInstance(makem e);
          List<string> itsMe = o as List<string>;
          Console.WriteLi ne((itsMe == null) ? "Failed" : "Succeeded" );

          [/code]
          EDIT: Since I found this topic interesting, I did this:
          [code=C#]
          using System.Reflecti on;

          public class GWorker
          {
          public Type BaseGenericType = null;
          public Type MakeableType = null;
          public Type[] typeArgs = new Type[0];
          public object InstanceObj = null;

          public GWorker(Type BaseType, Type[] CreationArgs)
          {
          typeArgs = CreationArgs;
          BaseGenericType = BaseType;
          MakeableType = BaseGenericType .MakeGenericTyp e(typeArgs);
          InstanceObj = Activator.Creat eInstance(Makea bleType);
          }
          public void Add(object[] ValuesForASingl eAdd)
          {

          MethodInfo wmi = MakeableType.Ge tMethod("Add");
          if (wmi != null)
          {
          wmi.Invoke(Inst anceObj, ValuesForASingl eAdd);
          }
          }
          }//end of class
          [/code]

          Then for use:
          [code=C#]
          GWorker gw= new GWorker(typeof( List<>),new Type[]{ typeof(string) });
          gw.Add(new string[] { "howdy" });
          gw.Add(new string[] { "dan" });
          gw.Add(new string[] { "fred" });
          List<string> lis = (List<string>)g w.InstanceObj;
          //lis.Count =3
          [/code]

          You could probably add a .Count similar to how i do that .Add() function if you wanted

          Comment

          Working...