我有一个像这样的方法调用序列:
//This is the Load method
BusinessLogic.Models.ProjectEngineer p = new BusinessLogic.Models.ProjectEngineer(project);
p.IsQualifiedFor(enType, educationEnum);
.
.
.
[UsesRule]
public static void IsQualifiedFor(this Models.ProjectEngineer p, EngineerTypeEnum enType, FieldEducationEnum fieldEducation)
{
RuleCache.EvaluateRule(p, enType, fieldEducation);
}
.
.
.
public static bool EvaluateRule<TClass>(TClass sourceObj, params object[] parameters)
{
var frame = new StackTrace().GetFrame(1);
var methodInfo = frame.GetMethod();
var atr = methodInfo.GetCustomAttribute<UsesRuleAttribute>();
if (atr == null)
throw new EISException("You can't call 'EvaluateRule' method in a method that does not have 'UsesRuleAttribute'.");
...
}
它在生产服务器中运行良好,直到今天我更改了一些不相关的内容。现在,EvaluateRule
方法抛出异常,因为它在调用者中找不到 UsesRuleAttribute
。这是异常的堆栈跟踪:
BusinessLogic.EISException: You can't call 'EvaluateRule' method in a method that does not have 'UsesRuleAttribute'.
at BusinessLogic.Rule.RuleCache.EvaluateRule[TClass](TClass sourceObj, Object[] parameters) in e:\EIS\EISMvc\BusinessLogic\Rule\RuleCache.cs:line 146
at EIS.Quota.Owner.EngineerList.Load() in e:\EIS\EISMvc\EIS\Quota\Owner\EngineerList.aspx.cs:line 66
如您所见,此处缺少 IsQualifiedFor
方法调用(它是一个扩展方法)。当我在我的机器上调试项目时,它工作正常。这是某种优化还是什么?
最佳答案
很可能是inlined由 JIT(即时编译器)处理,即在生成的 native 代码中被其主体替换,因此它不会出现在堆栈中。
您可以使用 MethodImpl
属性来防止内联:
[UsesRule]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void IsQualifiedFor(this Models.ProjectEngineer p, EngineerTypeEnum enType, FieldEducationEnum fieldEducation)
关于c# - 方法未显示在生产中的堆栈跟踪中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23007286/