Why does this line cause a VerificationException when running under .NET 4?

时光怂恿深爱的人放手 提交于 2019-12-03 09:54:50

The underlying reason for the error is a change in the signature of IsEnum.

In .NET 2.0 (and 3.0), IsEnum wasn't a virtual method:

public bool IsEnum { get; }

The assembly emitted to call it is:

call instance bool [mscorlib]System.Type::get_IsEnum()

In .NET 4.0, IsEnum is a virtual method:

public virtual bool IsEnum { get; }

Here is the same line of assembly for 4.0:

callvirt instance bool [mscorlib]System.Type::get_IsEnum()

The error you're getting was added in peverify just before the 2.0 release, and warns when a virtual method is called non-virtually.

Now, peverify loads up your code, loads .NET 4.0, and then checks your code. Since your code calls the (.NET 4.0) virtual method non-virtually, the error is shown.

One would think that since you're building against the .NET 2.0 version, this should be fine, and it would load the .NET 2.0 CLR to check. It doesn't seem so.

Edit:

In order to check this, I downloaded .NET 2.0's SDK and tried the peverify in there. It correctly verifies the code.

So the message would seem to be this: use a peverify which matches the target framework of your code.

Solution:

It seems that the _Type interface provides a solution to this:

if (((_Type)typeof(T)).IsEnum) ...

The documentation says it is designed to be called from unmanaged code, but as a side effect of it being an interface, it provides a stable (virtual) method to call.

I have confirmed that it works with peverify whether you target 2.0 or 4.0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!