Reflection and Generics (List) [modified]
-
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 -
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 -
You just specify the type between the angle brackets:
List<myType> lista = new List<myType>();
Now you have a list ofmyType
objects.--- single minded; short sighted; long gone;
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
-
You just specify the type between the angle brackets:
List<myType> lista = new List<myType>();
Now you have a list ofmyType
objects.--- single minded; short sighted; long gone;
He meant how to create List during runtime
#region signature my articles #endregion
-
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, 2007You 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
-
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
-
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
-
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
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.
-
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
-
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.
I think it should look like this:
ListOfMyType.InvokeMember("Add", BindingFlags.Public | BindingFlags.InvokeMethod, null, myObject, new object[] { myType1 } );
#region signature my articles #endregion
-
You are welcome :)
#region signature my articles #endregion
-
He meant how to create List during runtime
#region signature my articles #endregion
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;