方法未在生产中的堆栈跟踪中显示

问题描述 投票:2回答:1

我有一个这样的方法调用序列:

//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异常,因为它无法在调用UsesRuleAttribute找到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方法调用(它是扩展方法)。 当我在机器上调试项目时,它工作正常。 这是某种优化还是什么?

c# extension-methods stack-trace
1个回答
3
投票

它可能已由JIT(即时编译器) 内联 ,即在生成的本机代码中由其主体替换,因此它不会出现在堆栈中。

您可以通过使用MethodImpl属性来防止内联:

[UsesRule]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void IsQualifiedFor(this Models.ProjectEngineer p, EngineerTypeEnum enType, FieldEducationEnum fieldEducation)
© www.soinside.com 2019 - 2024. All rights reserved.