public class MyList : List<MyClass>
Run Code Online (Sandbox Code Playgroud)
MyClass如果我有一个object包含实例的实例,如何通过反射获取类型MyList?列表可以是空的,所以我不能做类似的事情myList[0].GetType().
ps我不能只是停止使用MyList并直接使用通用List(情况有点复杂,并且有"隐藏"泛型参数的原因),所以我无法MyClass通过GetGenericArguments().
var elementType = (
from iface in myList.GetType().GetInterfaces()
where iface.IsGenericType
where iface.GetGenericTypeDefinition() == typeof(IList<>)
select iface.GetGenericArguments()[0])
.Single();
Run Code Online (Sandbox Code Playgroud)
我使用IList<T>而不是列表.这更通用.但是,也存在实现多个ILis<T>版本(例如IList<string>和IList<int>)的类型的更改.
| 归档时间: |
|
| 查看次数: |
701 次 |
| 最近记录: |