我确实需要一个解决方案来加载对象列表-查找,其中只有一个属性是从当前对象引用的,如本例所示。class LookupObjectAddress{ [...] public string City { get; set; } [...]}class WorkingObject{ // references the property from LookupObjectAddress public string City { get; set; }}对于查找,我需要从数据库中加载一个列表,以知道从何处加载使用属性的信息class WorkingObject{ // references the property from LookupObjectAddress [Lookup(Type=typeof(LookupObjectAddress), staticloaderclass="LookupObjLoader", staticloaderMethod="LookupObjLoadMethod")] public string City { get; set; }}在读取workingobject.city属性的propertyinfo之后,我知道查找对象的类型,以及从哪个类加载它的方法。现在我需要桥得到一个有三个参数的列表。Type loaderClass = Type.GetType(classname);MethodInfo loaderMethod = loaderClass.GetMethod(loadmethod);object objList = loaderMethod.Invoke(null, new object[] {});因为我需要类型化列表来使用ui上lookupobjects的属性,所以如何在代码中成为一个可用列表?我理想的结果是,如果我能输入:var list = Loader.Load(type, "LookupObjLoader", "LookupObjLoadMethod");从属性中读取参数的位置。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 为了生成一个List<T>类型,从而在运行时生成一个实例,其中t来自一个Type对象(即编译时不知道),您可以这样做:Type genericListType = typeof(List<>); // yes, this really is legal syntaxType elementType = ...Type specificListType = genericListType.MakeGenericType(elementType);// specificListType now corresponds to List<T> where T is the same type// as elementTypeIList list = (IList)Activator.CreateInstance(specificListType);这将在运行时生成正确的列表类型并将其存储在list变量中。请注意,编译器无法推断变量类型,因此:var list = Loader.Load(...)仍然不会产生List<T>类型,它必须使用非泛型(编译时已知的类型)来存储列表,如IList,但您存储在其中的对象可以是泛型对象,产生方式如上所述。 (adsbygoogle = window.adsbygoogle || []).push({});