【发布时间】:2015-10-09 20:16:26
【问题描述】:
我创建了一个OnMethodBoundary 方面来跟踪模板方法中的进度事件。就整体进度而言,需要了解的一件事是我的流程在完成之前所需的进度步骤数,即用我的[AffectsProgress] 属性标记的方法数。
我尝试使用System.Reflection 来确定它,而不是对其进行硬编码并且在添加和删除方法时必须对其进行维护(我尝试了许多变体):
typeof (MyModuleWithProgressSteps)
.GetMethods(/* whatever BindingFlags I need */)
.SelectMany(x => x.CustomAttributes.Where(attribute => attribute.AttributeType == typeof (AffectsProgress)))
但是,即使我在删除 Where 子句时能够看到其他属性,但我无法找到任何 PostSharp 方面。我天真的猜测是,在执行时干扰调用堆栈的 PostSharp 方面实际上并不是传统的方法属性,因此 System.Reflection 库看不到它们。
有没有人知道我可以通过System.Reflection 或 PostSharp 本身获得我正在寻找的东西?我遇到了ReflectionSearch 和IAspectRepositoryService,它们需要终极版,但我不确定这些是否足够。
编辑:由@Daniel Balas 解决。使用该答案和我找到的here 的信息,我最终得到了一个看起来像这样的自定义方面,并且能够在运行时通过反射检测到:
[Serializable]
[MulticastAttributeUsage(PersistMetaData = true)]
internal class AffectsProgress : OnMethodBoundaryAspect
{
public override void OnExit(MethodExecutionArgs args)
{
// do all my progress-related stuff here
}
}
【问题讨论】:
标签: c# reflection postsharp