问题描述
我有类似以下内容的(C#)函数
I have a (C#) function similar to the following.
private static bool SpecialCase = false;
public void Foo()
{
if (SpecialCase)
{
InternalMethod();
return;
}
Console.WriteLine();
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void InternalMethod();
当我与调试器内的.NET框架4执行此,该方法成功地打印空白行控制台和回报。当我执行它在调试器外部,它抛出具有以下消息的异常:
When I execute this with the .NET Framework 4 inside the debugger, the method successfully prints blank line to the console and returns. When I execute it outside the debugger, it throws an exception with the following message:
System.Security.SecurityException: ECall methods must be packaged into a system module.
它出现的异常被抛出时,JIT编译器编译的方法,而不是当(如果) InternalMethod
被调用。有什么我可以做(如属性)告诉CLI要么不扔 SecurityException异常
,或延缓异常,直到该方法实际上是叫什么名字?
It appears the exception is thrown when the JIT compiler compiles the method as opposed to when (if) InternalMethod
is called. Is there anything I can do (e.g. attributes) to tell the CLI to either not throw the SecurityException
, or delay the exception until the method is actually called?
在用例旁注不同的(特定的)执行CLI的。当在Microsoft .NET框架中,调用 InternalMethod
运行是有效可达。
Side note on the use case: The SpecialCase
field is effectively false when running with Microsoft .NET Framework, and true when running under a different (specific) implementation of the CLI. When running under the Microsoft .NET Framework, the call to InternalMethod
is effectively unreachable.
推荐答案
您可能要检查的编译器指令作为一个可能的选项。
与采用一个运行时如果,这将确定呼叫是否包括在已编译的代码在所有的,而不是总是将其编译到代码,并试图确定是否把它在运行时(这是为时已晚,根据您的分析)。
您的用例似乎是一个测试/验证的情况下,这意味着你不需要它编进除非内部通话将实际进行编码。
请注意,如果你的用例涉及non-.NET运行时,你应该提供更多的信息,因为这可能会极大地更改正确的答案。
这篇关于SecurityException异常:紧急呼叫方法必须被打包成一个系统模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!