由网友(为爱、痴等)分享简介:我要调用限制输入类型T的通用方法来实现两个接口:I want to call a generic method that constrains the input type T to implement two interfaces:interface IA { }interface IB { }void fo...
我要调用限制输入类型T的通用方法来实现两个接口:
I want to call a generic method that constrains the input type T to implement two interfaces:
interface IA { }
interface IB { }
void foo<T>(T t) where T : IA, IB { }
我该如何解决的最后一行
How can I fix the last line of
void bar(object obj)
{
if (obj is IA && obj is IB)
{
foo((IA && IB)obj);
}
}
反射可能允许做的电话,但我想留在语言中。
Reflection probably allows to do the call, but I would like to stay within the language.
推荐答案
请问C#4.0动态关键字让你出狱的(主要)免费的吗?毕竟 - 你已经在做的类型检查
Does the C# 4.0 dynamic keyword get you out of jail (mostly) free? After all - you are already doing the type checking.
interface IC : IA, IB { }
void bar(object obj)
{
if (obj is IA && obj is IB)
{
IC x = (dynamic)obj;
foo(x);
}
}
这是否突破,如果FOO试图强制转换参数为T?我不知道。
Does that break if foo tries to cast the parameter to T? I don't know.
相关推荐
最新文章