Stu*_*etz 25 c# inheritance interface dynamic c#-4.0
在C#中,有谁知道为什么我不能做以下事情?(特别是下面标有"不好!"的行)
interface A
{
void Add(dynamic entity);
}
interface B : A {}
class C : B
{
public void Add(dynamic entity)
{
System.Console.WriteLine(entity);
}
}
class Program
{
static void Main(string[] args)
{
B b = new C();
dynamic x = 23;
b.Add(23); // fine
b.Add((int)x); // fine
(b as A).Add(x); // fine
//b.Add(x); // NOT fine!
}
}
Run Code Online (Sandbox Code Playgroud)
如果调用没有动态绑定,或者我显式地转换到层次结构根的接口,我绝对没有问题,但是动态绑定调用给了我:
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: No overload for method 'Add' takes '1' arguments
at CallSite.Target(Closure , CallSite , B , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
at Program.Main(String[] args) in C:\Users\Stuart\Documents\Visual Studio 2010\Projects\CSharp Testbed\Program.cs:line 218
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1275 次 |
| 最近记录: |