我有一个问题,通过强制通用列表。我获得了一个通用列表的值,并希望将其用作SomeMehtod
的参数。创建列表<T>通过类型
List<MyClass> GenericList;
var propList= this.GetType().GetProperty("GenericList").GetValue(this);
SomeMethode(propList) <-- Does not work
private void SomeMethode(List<T> genericList)
{
}
有人可以给我一个提示吗?我曾经试过,但它不会工作:
List<typeof(MyClass)> newPropList = propList;
我的问题是,MyClass
存储在类型变量:
var typesWithMyAttribute =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
let attributes = t.GetCustomAttributes(typeof(XMLDataAttribute), true)
where attributes != null && attributes.Length > 0
select new { Type = t, Attributes = attributes.Cast<XMLDataAttribute>() };
foreach (var a in typesWithMyAttribute)
{
var propList = this.GetType().GetProperty(a.Type.Name + "List").GetValue(this);
SomeMethode<a.Type>(propList); <-- Won't work
}
你能给出[MCVE]很难说出你想要达到的目标以及你得到的错误。 (“不起作用”并不像实际的错误信息那样有用。) –
'propList'是一个'object',你应该先施放它。 –
你应该把它作为'IList'而不是'List'来传递。你不知道'T'。但你知道它应该是一个'List <>' –