Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否可以使用属性将功能“混合”到类/方法/属性中。
就像是:
[TrackChanges] public Foo { get; set; }
如果可能的话,有谁会如何实施?
看看PostSharp,一个AOP框架。它是一个后编译器,它使用自定义属性向现有代码注入额外的行为。大多数示例通常是跟踪和安全性。
他们当然可以,但你必须使用反射来做到这一点。也仅仅因为你能做到并不意味着它很容易或优雅。
反射将使您可以访问任何类型的每个成员。您必须检索这些成员并通过 .NET 反射 API 来操作它们。虽然这可能有效,但维护或阅读并不是最容易的事情。