AOP/PostSharp 和脏检查 - 获取属性名称的更好方法?

发布于 2024-08-14 16:10:43 字数 1300 浏览 3 评论 0原文

我的方面:

  [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文