简体   繁体   English

如果 C# 属性像装饰器设计模式一样工作?

[英]If C# attribute work like decorator design pattern?

I have a question about attributes in C#, does it work like the decorator design pattern ?我有一个关于 C# 中的属性的问题,它是否像装饰器设计模式一样工作? For example the [HttpPost]例如[HttpPost]
attribute adds a behavior to call a decorated action ?属性添加一个行为来调用一个装饰动作? or does it work differently ?或者它的工作方式不同?

thank you in advance.先感谢您。

You cannot compare these two things.你不能比较这两件事。 Attributes are used to be accessed via reflection.属性用于通过反射访问。 They don't change behaviour magically.它们不会神奇地改变行为。 You might probably (ab)use attributes to implement some kind of weird decorator design pattern.您可能(ab)使用属性来实现某种奇怪的装饰器设计模式。 But just because an Attribute "decorates" eg a member, class, method or something, it has nothing to do with the decorator design pattern!但是仅仅因为一个属性“装饰”了一个成员、类、方法或其他东西,它与装饰器设计模式无关!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM