<tfoot id='Rk50Q'></tfoot>
      <bdo id='Rk50Q'></bdo><ul id='Rk50Q'></ul>
  • <i id='Rk50Q'><tr id='Rk50Q'><dt id='Rk50Q'><q id='Rk50Q'><span id='Rk50Q'><b id='Rk50Q'><form id='Rk50Q'><ins id='Rk50Q'></ins><ul id='Rk50Q'></ul><sub id='Rk50Q'></sub></form><legend id='Rk50Q'></legend><bdo id='Rk50Q'><pre id='Rk50Q'><center id='Rk50Q'></center></pre></bdo></b><th id='Rk50Q'></th></span></q></dt></tr></i><div id='Rk50Q'><tfoot id='Rk50Q'></tfoot><dl id='Rk50Q'><fieldset id='Rk50Q'></fieldset></dl></div>

    <small id='Rk50Q'></small><noframes id='Rk50Q'>

      1. <legend id='Rk50Q'><style id='Rk50Q'><dir id='Rk50Q'><q id='Rk50Q'></q></dir></style></legend>
      2. 在 .NET 中使用属性的性能开销

        Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)

            • <bdo id='8ZunA'></bdo><ul id='8ZunA'></ul>
            • <i id='8ZunA'><tr id='8ZunA'><dt id='8ZunA'><q id='8ZunA'><span id='8ZunA'><b id='8ZunA'><form id='8ZunA'><ins id='8ZunA'></ins><ul id='8ZunA'></ul><sub id='8ZunA'></sub></form><legend id='8ZunA'></legend><bdo id='8ZunA'><pre id='8ZunA'><center id='8ZunA'></center></pre></bdo></b><th id='8ZunA'></th></span></q></dt></tr></i><div id='8ZunA'><tfoot id='8ZunA'></tfoot><dl id='8ZunA'><fieldset id='8ZunA'></fieldset></dl></div>
              <tfoot id='8ZunA'></tfoot>

                <small id='8ZunA'></small><noframes id='8ZunA'>

              • <legend id='8ZunA'><style id='8ZunA'><dir id='8ZunA'><q id='8ZunA'></q></dir></style></legend>
                    <tbody id='8ZunA'></tbody>

                  本文介绍了在 .NET 中使用属性的性能开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  1.. 使用属性是否会导致性能开销?想想这样的课程:

                  1.. Is there any performance overhead caused by the usage of attributes? Think for a class like:

                      public class MyClass
                      {
                         int Count {get;set;}
                      }
                  

                  它有 10 个属性(属性是类,属性类本身比 MyClass 本身大得多,例如:

                  where it has 10 attibutes (attributes being classes, where the attribute classes themselves are way larger than MyClass itself like:

                  public class FirstAttribute : Attribute
                  {
                     int A,B,C,D,E,F,G,H,I,J ... {get;set;}
                  }
                  

                  2.. 每次实例化 MyClass 时,这些属性中的 10 个是否会成为内存开销?(FirstAttributeMyClass 大小的 10 倍,它将用其中的 10 个进行装饰,因此与属性的总大小相比,使实际对象本身非常小装饰.)这是一个问题吗?

                  2.. Would 10 of these attributes be a memory overhead everytime MyClass is instantiated? (FirstAttribute being 10 times the size of MyClass which will be decorated with 10 of these, so making the actual object itself so small compared to the total size of the attributes that are decorated on it.) Is this a concern?

                  3.. 这种情况对于结构体(结构体是值类型,属性是引用类型)会有什么不同吗?

                  3.. Would this scenario be any different for structs (Structs being value types and attributes being reference types)?

                  4.. 与它们所附加的对象相关的属性存储在内存中的什么位置?他们是怎么联系在一起的?

                  4.. Where are attibutes stored in memory in relation to the object that they are attached? How are they hooked together?

                  5.. MyClass 初始化后或使用反射检索属性时是否会立即初始化属性?

                  5.. Are attributes initialized as soon as MyClass is initialized or when you use reflection to retrieve them?

                  推荐答案

                  1. 在空间方面有一点点开销,但不是很多 - 属性不会妨碍正常执行.

                  1. There's a tiny bit of overhead in terms of space, but not a lot - attributes don't get in the way of normal execution.

                  不,属性作用于类型,而不是实例,因此使用大量大"属性不会占用大量内存.(我不知道你是每个 concrete 类型获得一个泛型,还是每个泛型类型定义一个 - 我希望是后者......)

                  No, attributes act on types, not instances, so you won't take up a vast amount of memory by using lots of "big" attributes. (I don't know whether you get one per concrete type for generics, or one per generic type definition - I'd expect the latter...)

                  不,因为 1 的答案.

                  No, because of the answer to 1.

                  属性不附加到对象 - 它们附加到类型.我不知道它们在内存中存储位置的确切细节,但无论如何这是一个实现细节.

                  Attributes aren't attached to objects - they're attached to types. I don't know the details of exactly where they're stored in memory, but that's an implementation detail anyway.

                  只有在使用反射时才会初始化属性.

                  Attributes are only initialized when you use reflection.

                  这篇关于在 .NET 中使用属性的性能开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                  Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)
                  C# Attributes and their uses(C# 属性及其用途)
                • <small id='vZ97S'></small><noframes id='vZ97S'>

                    <tbody id='vZ97S'></tbody>

                    <i id='vZ97S'><tr id='vZ97S'><dt id='vZ97S'><q id='vZ97S'><span id='vZ97S'><b id='vZ97S'><form id='vZ97S'><ins id='vZ97S'></ins><ul id='vZ97S'></ul><sub id='vZ97S'></sub></form><legend id='vZ97S'></legend><bdo id='vZ97S'><pre id='vZ97S'><center id='vZ97S'></center></pre></bdo></b><th id='vZ97S'></th></span></q></dt></tr></i><div id='vZ97S'><tfoot id='vZ97S'></tfoot><dl id='vZ97S'><fieldset id='vZ97S'></fieldset></dl></div>
                      <bdo id='vZ97S'></bdo><ul id='vZ97S'></ul>
                      <tfoot id='vZ97S'></tfoot>

                        1. <legend id='vZ97S'><style id='vZ97S'><dir id='vZ97S'><q id='vZ97S'></q></dir></style></legend>