c# - 如何从 PostSharp 事件中获取特定于上下文的数据
问题描述
我正在考虑使用PostSharp
它来捕获发生的事件。然而,我看到的一个大问题是Event
我系统中的一部分至少是改变元素的主键。
当我定义我的自定义属性时,我可以要求某些数据点,但它们都是static。我可以建立我的属性来知道如何访问 pk 吗?
就像是
[Event(GetPrimaryKey = (Dictionary<string, object> args)=> { return args["UserId"]; })]
public string CreateUser()
{
...
}
在这种情况下可以使用
public override void OnSuccess(MethodExecutionArgs args)
{
var key = this.getPrimaryKey(args.Arguments);
}
解决方案
这是可能的,但您需要采取一些不同的方法,因为您可以在属性规范中设置的值相当有限。
最好的方法是使用属性标记主键参数,然后在 PostSharp 构建时找到您需要的参数并将其位置存储在方面。然后该方面被序列化,您可以在运行时使用此变量。
[AttributeUsage(AttributeTargets.Parameter)]
public class EventKeyAttribute : Attribute
{
}
[PSerializable]
public class EventAttribute : OnMethodBoundaryAspect
{
private int keyPosition;
public override void CompileTimeInitialize( MethodBase method, AspectInfo aspectInfo )
{
this.keyPosition = -1;
// Go through method's arguments and find the key position.
foreach (var param in method.GetParameters())
{
if (param.IsDefined(typeof(EventKeyAttribute)))
{
if (this.keyPosition != -1)
{
// Build time error.
Message.Write( param, SeverityType.Error, "ERR001", $"Multiple parameters of {method} are marked with [EventKey]." );
return;
}
this.keyPosition = param.Position;
}
}
if (this.keyPosition == -1)
{
// Build time error.
Message.Write( method, SeverityType.Error, "ERR002", $"No parameter of {method} is marked with [EventKey]." );
}
}
public override void OnSuccess( MethodExecutionArgs args )
{
Console.WriteLine( $"Key is: {args.Arguments[this.keyPosition]}" );
}
}
之后,您可以简单地在方法上应用这两个属性:
[Event]
static void Foo(int a, [EventKey]int b, int c)
{
}
推荐阅读
- apache - 尝试访问 Wordpress 安装上的图像时出现“403 禁止错误”
- spring - 创建名为“personRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 com.mongodb.util.JSONParseException:
- python - 是否有任何原因导致此代码在某些 Python 代码的开头不起作用?
- ios - RPScreenRecorder startCapture 不在后台录制
- vb.net - 在 Visual Studio 上更新数据库源的问题
- excel - 获取 Workbook.Open 以每天安排我的宏(不仅仅是一次)
- python - Python Eve Auth 无法返回 401 异常
- asp.net - 允许应用程序在没有用户名/密码的情况下访问受 Azure AD 保护的 URL
- pandas - 从字典和索引顺序创建 DataFrame?
- angular - 将 Angular 应用程序更新到版本 7 后,应用程序不会在根目录中的文件夹中加载 css 和 json 文件