Evo kako mozes dodati vrednosti generickoj listi preko Reflectiona a da ne koristis MSIL ...
Kod:
private void button1_Click(object sender, EventArgs e)
{
//Ucitavanje DLL-a sa diska u AppDomain
Assembly asm = Assembly.LoadFile(@"C:\MojDll.dll");
//Ucitavanje klase
Type myType = asm.GetType(Path.GetFileNameWithoutExtension(asm.ManifestModule.ScopeName) +".Class1");
//Instanciranje klase preko staticke metode CreateInstance
object vrednost1 = Activator.CreateInstance(myType);
object vrednost2 = Activator.CreateInstance(myType);
object vrednost3 = Activator.CreateInstance(myType);
//Dodavanje vrednosti polja
vrednost1.GetType().GetProperty("Polje").SetValue(vrednost1, 1, null);
vrednost2.GetType().GetProperty("Polje").SetValue(vrednost2, 2, null);
vrednost3.GetType().GetProperty("Polje").SetValue(vrednost3, 3, null);
//Kreiranje Liste za nasu klasu
Type listType = typeof(List<>);
Type listClass = listType.MakeGenericType(myType);
object obj = Activator.CreateInstance(listClass);
//MethodInfo[] methods = myObject.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public);
//Dodavanje vrednosti generickoj listi
DodajVrednosti(ref obj, vrednost1, vrednost2, vrednost3);
}
/// <summary>
/// Dodavanje vrednosti generickoj listi
/// </summary>
/// <param name="obj">List<T> objekat</param>
/// <param name="vrednosti">Instance klase</param>
private void DodajVrednosti(ref object obj, params object[] vrednosti)
{
for(int i=0;i<vrednosti.Length;i++)
{
obj.GetType().InvokeMember("Add",
BindingFlags.InvokeMethod |
BindingFlags.Public |
BindingFlags.Instance,
null,
obj,
new object[] { vrednosti[i] } );
}
}
Kod:
public class Class1
{
private int polje;
public int Polje
{
get{return this.polje;}
set {this.polje = value;}
}
}