Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reflection and Generics (List) [modified]

Reflection and Generics (List) [modified]

Scheduled Pinned Locked Moved C#
tutorialquestion
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Guffa

    You just specify the type between the angle brackets: List<myType> lista = new List<myType>(); Now you have a list of myType objects.

    --- single minded; short sighted; long gone;

    V Offline
    V Offline
    Vertyg0
    wrote on last edited by
    #3

    You can`t do that ! Error 1 The type or namespace name 'myType' could not be found (are you missing a using directive or an assembly reference?) D:\Documents and Settings\IvanM\My Documents\Visual Studio 2005\Projects\WindowsApplication4\WindowsApplication4\Form1.cs 24 18 WindowsApplication4

    1 Reply Last reply
    0
    • G Guffa

      You just specify the type between the angle brackets: List<myType> lista = new List<myType>(); Now you have a list of myType objects.

      --- single minded; short sighted; long gone;

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #4

      He meant how to create List during runtime

      #region signature my articles #endregion

      G 1 Reply Last reply
      0
      • V Vertyg0

        How to make generic list at runtime out of this example: Assembly asm = Assembly.LoadFile(@"C:\MyDll.dll"); Type myType = asm.GetType("WindowsApplication4.Class1"); Now i dont know how to create generic List from myType any idea? With no reflection it would be like List lista = new List(), but how to make it with reflection? Thanks. -- modified at 9:51 Saturday 15th September, 2007

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #5

        You can do it like this: Assembly asm = Assembly.LoadFile(@"C:\MyDll.dll"); Type myType = asm.GetType("WindowsApplication4.Class1"); Type ListType = typeof(List<>); Type ListOfMyType = ListType.MakeGenericType(myType);

        #region signature my articles #endregion

        V 1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          You can do it like this: Assembly asm = Assembly.LoadFile(@"C:\MyDll.dll"); Type myType = asm.GetType("WindowsApplication4.Class1"); Type ListType = typeof(List<>); Type ListOfMyType = ListType.MakeGenericType(myType);

          #region signature my articles #endregion

          V Offline
          V Offline
          Vertyg0
          wrote on last edited by
          #6

          Thanks Giorgi, only one question remains how to add objects to ListOfMyType? Thanks alot!

          G 1 Reply Last reply
          0
          • V Vertyg0

            Thanks Giorgi, only one question remains how to add objects to ListOfMyType? Thanks alot!

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #7

            First you will need to create instance of ListOfMyType. You can do it like this: object myObject = Activator.CreateInstance(ListOfMyType); Then use InvokeMember method of Type class to invoke any method of ListOfMyType class. Here is an example from MSDN: Type.InvokeMember Method[^]

            #region signature my articles #endregion

            V 2 Replies Last reply
            0
            • G Giorgi Dalakishvili

              First you will need to create instance of ListOfMyType. You can do it like this: object myObject = Activator.CreateInstance(ListOfMyType); Then use InvokeMember method of Type class to invoke any method of ListOfMyType class. Here is an example from MSDN: Type.InvokeMember Method[^]

              #region signature my articles #endregion

              V Offline
              V Offline
              Vertyg0
              wrote on last edited by
              #8

              I think that you can`t invoke Add member Code: myObject.GetType().InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod, null, myObject, new object[] { myType1 } ); Error: Method 'System.Collections.Generic.List`1[[MojDll.Class1, MojDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Add' not found.

              G 1 Reply Last reply
              0
              • G Giorgi Dalakishvili

                First you will need to create instance of ListOfMyType. You can do it like this: object myObject = Activator.CreateInstance(ListOfMyType); Then use InvokeMember method of Type class to invoke any method of ListOfMyType class. Here is an example from MSDN: Type.InvokeMember Method[^]

                #region signature my articles #endregion

                V Offline
                V Offline
                Vertyg0
                wrote on last edited by
                #9

                Ah didn`t created instance of class .... everything works now thanks !

                G 1 Reply Last reply
                0
                • V Vertyg0

                  I think that you can`t invoke Add member Code: myObject.GetType().InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod, null, myObject, new object[] { myType1 } ); Error: Method 'System.Collections.Generic.List`1[[MojDll.Class1, MojDll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Add' not found.

                  G Offline
                  G Offline
                  Giorgi Dalakishvili
                  wrote on last edited by
                  #10

                  I think it should look like this: ListOfMyType.InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod, null, myObject, new object[] { myType1 } );

                  #region signature my articles #endregion

                  1 Reply Last reply
                  0
                  • V Vertyg0

                    Ah didn`t created instance of class .... everything works now thanks !

                    G Offline
                    G Offline
                    Giorgi Dalakishvili
                    wrote on last edited by
                    #11

                    You are welcome :)

                    #region signature my articles #endregion

                    1 Reply Last reply
                    0
                    • G Giorgi Dalakishvili

                      He meant how to create List during runtime

                      #region signature my articles #endregion

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #12

                      I see. Why would anyone want to do that? Why not just using a List<object>? Better yet, why not make an interface that the class can implement, so that you can create a list of that interface, and don't have to use reflection for anything that has to do with the objects.

                      --- single minded; short sighted; long gone;

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups