1
的通用对象我有一些简单的代码这说明我的问题:上溯造型到从System.Object
private void InitializeOther()
{
List<Foo> list = new List<Foo>();
Casting(list);
}
//in the "real" case I have no knowledge of o, other than it could be a List<>
private void Casting(object o)
{
Type t = o.GetType();
while (t.BaseType != typeof(Object))
{
if (t.IsGenericType && typeof(List<>) == t.GetGenericTypeDefinition())
{
//now I know that o is of type List<>. How can I now access List<> members from o?
break;
}
t = t.BaseType;
}
}
所以,我可以肯定的是对象o是(或派生)从List<T>
,但现在我希望能够在o上访问List<T>
成员,这意味着将其最高可达List<Foo>
。在我的“真实”情况下,我不了解Foo。
我很确定它可以完成,如果你知道如何做,我会非常感激,如果你能与我分享你的知识!
好的,谢谢,方法调用它是那么 – 2009-02-17 11:35:13