我有一个这样的方法调用序列:
//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
方法调用(它是扩展方法)。 当我在机器上调试项目时,它工作正常。 这是某种优化还是什么?
它可能已由JIT(即时编译器) 内联 ,即在生成的本机代码中由其主体替换,因此它不会出现在堆栈中。
您可以通过使用MethodImpl
属性来防止内联:
[UsesRule]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void IsQualifiedFor(this Models.ProjectEngineer p, EngineerTypeEnum enType, FieldEducationEnum fieldEducation)