例:
public static void DoSomething<K,V>(IDictionary<K,V> items) {
items.Keys.Each(key => {
if (items[key] **is IEnumerable<?>**) { /* do something */ }
else { /* do something else */ }
}
Run Code Online (Sandbox Code Playgroud)
这可以不使用反射吗?我怎么说C#中的IEnumerable?我应该只使用IEnumerable,因为IEnumerable <>实现IEnumerable?
小智 107
非常感谢这篇文章.我想提供一个对我来说效果更好的Konrad Rudolph解决方案.我在该版本中遇到了一些小问题,特别是在测试Type是否为可为空的值类型时:
public static bool IsAssignableToGenericType(Type givenType, Type genericType)
{
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
{
if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
return true;
}
if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
return true;
Type baseType = givenType.BaseType;
if (baseType == null) return false;
return IsAssignableToGenericType(baseType, genericType);
}
Run Code Online (Sandbox Code Playgroud)
Kon*_*lph 40
以前接受的答案很好但是错了.值得庆幸的是,错误很小.IEnumerable
如果你真的想知道接口的通用版本,那么检查是不够的; 有很多类只实现非泛型接口.我会在一分钟内给出答案.首先,我想指出接受的答案过于复杂,因为以下代码在给定的情况下会达到相同的效果:
if (items[key] is IEnumerable)
Run Code Online (Sandbox Code Playgroud)
这甚至更多,因为它分别适用于每个项目(而不是它们的公共子类V
).
现在,为了正确的解决方案.这有点复杂,因为我们必须采用泛型类型IEnumerable`1
(即IEnumerable<>
具有一个类型参数的类型)并注入正确的泛型参数:
static bool IsGenericEnumerable(Type t) {
var genArgs = t.GetGenericArguments();
if (genArgs.Length == 1 &&
typeof(IEnumerable<>).MakeGenericType(genArgs).IsAssignableFrom(t))
return true;
else
return t.BaseType != null && IsGenericEnumerable(t.BaseType);
}
Run Code Online (Sandbox Code Playgroud)
您可以轻松地测试此代码的正确性:
var xs = new List<string>();
var ys = new System.Collections.ArrayList();
Console.WriteLine(IsGenericEnumerable(xs.GetType()));
Console.WriteLine(IsGenericEnumerable(ys.GetType()));
Run Code Online (Sandbox Code Playgroud)
收益率:
True
False
Run Code Online (Sandbox Code Playgroud)
不要过分担心这会使用反射.虽然这确实增加了运行时开销,但is
运算符的使用也是如此.
当然,上面的代码非常有限,可以扩展为更普遍适用的方法IsAssignableToGenericType
.以下实现略有不正确1,我将其留在这里仅用于历史目的.不要使用它.相反,詹姆斯在他的回答中提供了一个优秀,正确的实现.
public static bool IsAssignableToGenericType(Type givenType, Type genericType) {
var interfaceTypes = givenType.GetInterfaces();
foreach (var it in interfaceTypes)
if (it.IsGenericType)
if (it.GetGenericTypeDefinition() == genericType) return true;
Type baseType = givenType.BaseType;
if (baseType == null) return false;
return baseType.IsGenericType &&
baseType.GetGenericTypeDefinition() == genericType ||
IsAssignableToGenericType(baseType, genericType);
}
Run Code Online (Sandbox Code Playgroud)
1当它出现故障genericType
是一样的givenType
; 出于同样的原因,它可以为可空类型,即
IsAssignableToGenericType(typeof(List<int>), typeof(List<>)) == false
IsAssignableToGenericType(typeof(int?), typeof(Nullable<>)) == false
Run Code Online (Sandbox Code Playgroud)
我用一套全面的测试用例创建了一个要点.
关于泛型类型和使用IsAssignableFrom()的警告......
说你有以下内容:
public class MyListBase<T> : IEnumerable<T> where T : ItemBase
{
}
public class MyItem : ItemBase
{
}
public class MyDerivedList : MyListBase<MyItem>
{
}
Run Code Online (Sandbox Code Playgroud)
在基本列表类型或派生列表类型上调用IsAssignableFrom将返回false,但显然是MyDerivedList
继承MyListBase<T>
.(Jeff的快速说明,泛型绝对必须包含在代码块中或倾斜以获得<T>
,否则它被省略.这是有意吗?)问题源于MyListBase<MyItem>
被视为完全不同类型的事实MyListBase<T>
.以下文章可以更好地解释这一点. http://mikehadlow.blogspot.com/2006/08/reflecting-generics.html
相反,请尝试以下递归函数:
public static bool IsDerivedFromGenericType(Type givenType, Type genericType)
{
Type baseType = givenType.BaseType;
if (baseType == null) return false;
if (baseType.IsGenericType)
{
if (baseType.GetGenericTypeDefinition() == genericType) return true;
}
return IsDerivedFromGenericType(baseType, genericType);
}
Run Code Online (Sandbox Code Playgroud)
/编辑:Konrad的新帖子考虑了通用递归以及接口.非常好的工作.:)
/ EDIT2:如果检查genericType是否是接口,则可以实现性能优势.检查可以是当前接口代码的if块,但如果您对使用.NET 3.5感兴趣,我的一位朋友提供以下内容:
public static bool IsAssignableToGenericType(Type givenType, Type genericType)
{
var interfaces = givenType.GetInterfaces().Where(it => it.IsGenericType).Select(it => it.GetGenericTypeDefinition());
var foundInterface = interfaces.FirstOrDefault(it => it == genericType);
if (foundInterface != null) return true;
Type baseType = givenType.BaseType;
if (baseType == null) return false;
return baseType.IsGenericType ?
baseType.GetGenericTypeDefinition() == genericType :
IsAssignableToGenericType(baseType, genericType);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9419 次 |
最近记录: |