这是使用属性在.net(C#)贵吗? [英] Is this use of attributes in .Net (C#) expensive?

查看:98
本文介绍了这是使用属性在.net(C#)贵吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在.net中的属性的使用,特别是C#,无论是昂贵的,为什么或者为什么不?

I would like to know whether the usage of Attributes in .Net, specifically C#, is expensive, and why or why not?

我问有关C#具体而言,除非有不同的.NET语言之间没有什么区别(因为基类库是一样的吗?)。

I am asking about C# specifically, unless there is no difference between the different .Net languages (because the base class libraries are the same?).

所有的新的.Net技术广泛使用的属性,如LINQ到SQL,ASP.Net MVC,WCF,企业库等,我想知道什么样的影响,这将有表现。该班的很多都会自动具有特定属性的装饰,或者需要某些功能/特性这些属性。

All the newer .Net technologies make extensive use of attributes, such as Linq to SQL, ASP.Net MVC, WCF, Enterprise Library, etc, and I was wondering what effect this would have on performance. Alot of the classes get automatically decorated with certain Attributes, or these attributes are required for certain functionality/features.

难道费用的问题取决于执行的具体细节?如何被编译为IL的属性?他们是自动缓存,或者是这件事的实现者?

Does the question of expense depend on implementation specific details? How are Attributes compiled to IL? Are they cached automatically, or is this up to the implementor?

推荐答案

属性的用法太模糊。取回的属性是反射操作有效的 - 你不会希望定期做一个循环 - 但他们没有昂贵的元数据包括,和典型使用模式(IMO)是建立其他一些重$ P看完属性后一度$ psentation(例如在内存架构)。​​

"The usage of attributes" is too vague. Fetching the attributes is a reflection operation effectively - you wouldn't want to regularly do it in a loop - but they're not expensive to include in the metadata, and the typical usage pattern (IMO) is to build some other representation (e.g. an in-memory schema) after reading the attributes once.

有可能是一些涉及到高速缓存,但我可能会缓存等再presentation反正。举例来说,如果我有描述装饰枚举值,我一般取属性的一次的建立一个字符串枚举字典(反之亦然)。

There may well be some caching involved, but I'd probably cache the other representation anyway. For example, if I were decorating enum values with descriptions, I'd generally fetch the attributes once to build a string to enum dictionary (or vice versa).

这篇关于这是使用属性在.net(C#)贵吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆