Che*_*hen 30 .net c# delegates cil anonymous-methods
请参阅以下代码:
public abstract class Base
{
public virtual void Foo<T>() where T : class
{
Console.WriteLine("base");
}
}
public class Derived : Base
{
public override void Foo<T>()
{
Console.WriteLine("derived");
}
public void Bang()
{
Action bang = new Action(delegate { base.Foo<string>(); });
bang(); //VerificationException is thrown
}
}
Run Code Online (Sandbox Code Playgroud)
new Derived().Bang();
抛出一个例外.在Bang
我生成的方法生成的CIL里面:
call instance void ConsoleApp.Derived::'<>n__FabricatedMethod1'<string>()
Run Code Online (Sandbox Code Playgroud)
和编译器生成方法的签名:
method private hidebysig
instance void '<>n__FabricatedMethod1'<T> () cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
01 00 00 00
)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void ConsoleApp.Base::Foo<!!T>()
IL_0006: ret
}
Run Code Online (Sandbox Code Playgroud)
我认为正确的代码应该是'<>n__FabricatedMethod1'<class T>
.这是一个错误吗?顺便说一句,没有使用delegate{ }
(lambda表达式是相同的),代码可以正常使用语法糖.
Action good = new Action(base.Foo<string>());
good(); //fine
Run Code Online (Sandbox Code Playgroud)
编辑我在windows8 RTM,.net framework 4.5中使用VS2012 RTMRel
编辑此错误现已修复.
归档时间: |
|
查看次数: |
1040 次 |
最近记录: |