c# - Generic method with unspecified type possible? -
I need a solution to load a list of objects - lookup where only one property is referenced from the existing object .
class LookupObjectAddress {[...] public string city {get; Set; } [...]} class working object {// lookup object ederation refers to property from public string {city} {get; Set; }}
For the lookup, I need a list to be loaded from the database, to know what attribute I use, to load it
class working object {// lookup object eder [lookup (type = typeoff (lookup object attender), staticloaderclass = "lookup obz loader", staticloader method = "lookup oblige pagepad") refers to property String City To obtain; Set; }}
After reading PropertyInfo for WorkObject.City property, I know the type of lookup object, and from which class it has the method to load it. A bridge is required to get a list to work with.
type loaderClass = Type.GetType (classname); MethodInfo Loader Method = Loader Class.GetMath (LoadMap); Object objList = loaderMethod.Invoke (empty, new object [] {});
Since I need a type list to use the properties of the lookup objectives on the UI, how can I become a usable list in the code?
My ideal result may be, if I can simply type:
var list = Loader.Load (type, "lookup objoller", "lookup objection" );
Where the parameters are read from the attribute.
In these, a type
object (not known in compile-time) From a list & lt; T & gt; Type
, and so on, to produce at runtime, you will do this:
type genericListType = typeof (list
); // Yes, this is really a legal syntax type typeType = ... type typical list type = normal list type. Mechanical type (element type); // Specific list type now list & lt; T & gt; Where t is the same type // As the element type isylist list = (iii) the catalyst. Createinstance (typical list type);
On this runtime, will produce the correct list type and store it in the list
variable.
Note that there is no way you can get the compiler to guess the variable type, so it is:
var list = loader.load (. ..)
will still not produce list & lt; T & gt;
Type , to store a list, it must use a non-normal, known-on-compile-time type, such as IList
, but which The object you store in may be a normal one, the way I describe above.
Comments
Post a Comment