I do need a solution for loading lists of objects – lookups where only one property is referenced from the current object as in this example.
class LookupObjectAddress
{
[...]
public string City
{ get; set; }
[...]
}
class WorkingObject
{
// references the property from LookupObjectAddress
public string City
{ get; set; }
}
For the lookup I need a List to be loaded from the database, to know from where to load the information I use an Attribute
class WorkingObject
{
// references the property from LookupObjectAddress
[Lookup(Type=typeof(LookupObjectAddress), staticloaderclass="LookupObjLoader", staticloaderMethod="LookupObjLoadMethod")]
public string City
{ get; set; }
}
After reading out the PropertyInfo for the WorkingObject.City Property I know the type of the lookup object, and from which class with which method to load it.
Now I need the bridge to get a List with that three parameters to work with.
Type loaderClass = Type.GetType(classname);
MethodInfo loaderMethod = loaderClass.GetMethod(loadmethod);
object objList = loaderMethod.Invoke(null, new object[] {});
Since I need the typed List<> for using properties of the LookupObjects on the UI, how can I become a useable list in Code?
My ideal Outcome would be, if I could just type:
var list = Loader.Load(type, "LookupObjLoader", "LookupObjLoadMethod");
where the parameters are read from the Attribute.
In order to produce a
List<T>type, and thus instance, at runtime, where T comes from aTypeobject (ie. not known at compile-time), you would do this:This will produce, at runtime, the correct list type and store it in the
listvariable.Note that there is no way you can get the compiler to infer the variable type, so this:
will still not produce a
List<T>type, it will have to use a non-generic, known-at-compile-time type to store the list, likeIList, but the object you store in it can be a generic one, produced the way I describe above.