I would like to return a List of entities via reflection.
It is not working in my project code but is working in a sandbox project. I am at a loss as to what is different between the two is. It is difficult to tear the project apart to find out why. Hopefully the following information will help someone point out a solution.
Project code not working
The following is a watch of my not working project code. Pardon the wide printscreens.
Different screen shots of different branches of the same result. My items are there but I cannot access them in a internal/public way. See the other errors.
click for larger - items listed
click for larger - error on List
click for larger - entry point not found
Working Sandbox
The following is an example of my WORKING sandbox.
Console
class Program
{
static void Main(string[] args)
{
string fullPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "\\")), "ClassLibrary1.dll");
AppDomain dom = AppDomain.CreateDomain("some");
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = fullPath;
Assembly assembly = dom.Load(assemblyName);
Type superclass = assembly.GetType("ClassLibrary1.MySuperClass");
Type superentity = assembly.GetType("ClassLibrary1.MySuperEntity");
object instance = Activator.CreateInstance(superclass, new object[]{"Hello"});
MethodInfo m = superclass.GetMethod("GetList", new Type[] {typeof (string)});
MethodInfo g = m.MakeGenericMethod(superentity);
var x = g.Invoke(instance, new object[] {"HelloEntity" });
}
}
ClassLibrary1.Class1.cs
namespace ClassLibrary1
{
public class MySuperEntity : MyBaseEntity
{
public MySuperEntity() {}
public MySuperEntity(string title) : base(title) {}
}
public class MyBaseEntity
{
public MyBaseEntity() {}
public MyBaseEntity(string title)
{
Title = title;
}
public string Title { get; set; }
}
public class MySuperClass : MyBaseClass {}
public class MyBaseClass
{
public List<T> GetList<T>(string msg) where T : MyBaseEntity
{
return new List<T>
{
Activator.CreateInstance(typeof(T), new object[] { "One " + msg }) as T,
Activator.CreateInstance(typeof(T), new object[] { "Two " + msg }) as T,
Activator.CreateInstance(typeof(T), new object[] { "Three " + msg }) as T
};
}
}
}
Aucun commentaire:
Enregistrer un commentaire