Mar*_*nox 3 c# generics ienumerable interface
我有一个方法接受一个 IEnumerable<dynamic>
有没有办法测试方法中的动态类型,看它是否实现了特定的接口?
我的意图是这样的:
public void myMethod(IEnumerable<dynamicT> enumerable)
{
if (dynamicT is IInterface)
{
// do one thing
}
else if (dynamicT is IAnotherInterface)
{
// do another thing
}
}
Run Code Online (Sandbox Code Playgroud)
更新
我应该包括这个重要的事实:<T>是动态的.
请原谅,我正在接近午夜,我正在编码.: - $
你可以使用Type.IsAssignableFrom方法:
确定是否可以从指定Type的实例分配当前Type的实例.
public void myMethod(IEnumerable<T> enumerable)
{
if (typeof(IInterface).IsAssignableFrom(typeof(T))
{
// do one thing
}
else if (typeof(IAnotherInterface).IsAssignableFrom(typeof(T))
{
// do another thing
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |