AOP/PostSharp 和脏检查 - 获取属性名称的更好方法?
我的方面:
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Class, Inheritance = MulticastInheritance.Strict)]
public sealed class NotifyPropertyChangedAttribute : InstanceLevelAspect
{
[OnLocationSetValueAdvice, MulticastPointcut(Targets = MulticastTargets.Property)]
public void OnPropertySet(LocationInterceptionArgs args)
{
if (args.Value == args.GetCurrentValue()) return;
args.ProceedSetValue();
IPersistent iENtity = this.Instance as IPersistent;
if (iENtity != null)
{
// do something with iENtity
// need to identify property name without using reflection
}
}
}
我已将此 NotifyPropertyChangedAttribute 应用于整个类,以便我可以处理属性更改,一切都按预期进行。
但我想知道已更改的属性的名称(这样我可以将脏属性的名称存储在某个集合中)。我想使用 LocationInterceptionArgs::Location 但我读到了这篇文章:
"Using this property causes the aspect weaver to generate code that has non-trivial runtime overhead."
有没有其他方法可以在我的方面获取受影响属性的名称,但不使用反射或其他具有大量运行时开销的代码?
有什么想法(或更好的例子)?也许我可以用其他方式实现它?我只需要某种方法来在运行时识别属性,即使没有名称 - 只是一些我可以用来在某些 Dictionary<> 中查找属性的值。稍后...
谢谢。
My aspect:
[Serializable]
[MulticastAttributeUsage(MulticastTargets.Class, Inheritance = MulticastInheritance.Strict)]
public sealed class NotifyPropertyChangedAttribute : InstanceLevelAspect
{
[OnLocationSetValueAdvice, MulticastPointcut(Targets = MulticastTargets.Property)]
public void OnPropertySet(LocationInterceptionArgs args)
{
if (args.Value == args.GetCurrentValue()) return;
args.ProceedSetValue();
IPersistent iENtity = this.Instance as IPersistent;
if (iENtity != null)
{
// do something with iENtity
// need to identify property name without using reflection
}
}
}
I have applied this NotifyPropertyChangedAttribute to entire class so I can process property changes, everything works as expected.
But I would like to know the name of the property which was changed (so I can store the names of dirty properties in some collection). I wanted to use LocationInterceptionArgs::Location but I read this:
"Using this property causes the aspect weaver to generate code that has non-trivial runtime overhead."
Is there any other way how I can get the name of affected property in my aspect but without using reflection or other code which has big runtime overhead?
Any ideas (or better - examples)? Maybe I can implement it some other way? I just need some way to identify the property at runtime, even without a name - just some value which I can use to find the property in some Dictionary<> later...
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论