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

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

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

        .NET 属性:为什么 GetCustomAttributes() 每次都创建一个新的属性实例?

        .NET Attributes: Why does GetCustomAttributes() make a new attribute instance every time?(.NET 属性:为什么 GetCustomAttributes() 每次都创建一个新的属性实例?)
      2. <tfoot id='gzos9'></tfoot>
          <legend id='gzos9'><style id='gzos9'><dir id='gzos9'><q id='gzos9'></q></dir></style></legend>
          1. <small id='gzos9'></small><noframes id='gzos9'>

              <bdo id='gzos9'></bdo><ul id='gzos9'></ul>

                    <tbody id='gzos9'></tbody>

                • <i id='gzos9'><tr id='gzos9'><dt id='gzos9'><q id='gzos9'><span id='gzos9'><b id='gzos9'><form id='gzos9'><ins id='gzos9'></ins><ul id='gzos9'></ul><sub id='gzos9'></sub></form><legend id='gzos9'></legend><bdo id='gzos9'><pre id='gzos9'><center id='gzos9'></center></pre></bdo></b><th id='gzos9'></th></span></q></dt></tr></i><div id='gzos9'><tfoot id='gzos9'></tfoot><dl id='gzos9'><fieldset id='gzos9'></fieldset></dl></div>
                  本文介绍了.NET 属性:为什么 GetCustomAttributes() 每次都创建一个新的属性实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我在 .NET 中使用了更多属性,并意识到每次调用 Type.GetCustomAttributes() 都会创建我的属性的一个新实例.这是为什么?我认为属性实例基本上是一个单例每个成员信息,有 1 个实例绑定到类型、PropertyInfo 等...

                  So I was playing around a little more with attributes in .NET, and realized that every call to Type.GetCustomAttributes() creates a new instance of my attribute. Why is that? I would think that the attribute instance would basically be a singleton-per-MemberInfo, with 1 instance bound to the Type, PropertyInfo, etc...

                  这是我的测试代码:

                  using System;
                  
                  namespace AttribTest
                  {
                  [AttributeUsage(AttributeTargets.Class)]
                  class MyAttribAttribute : Attribute
                  {
                      public string Value { get; set; }
                  
                      public MyAttribAttribute()
                          : base()
                      {
                          Console.WriteLine("Created MyAttrib instance");
                      }
                  }
                  
                  [MyAttrib(Value = "SetOnClass")]
                  class MyClass
                  {
                  }
                  
                  class Program
                  {
                      static void Main(string[] args)
                      {
                          Console.WriteLine("Getting attributes for MyClass.");
                          object[] a = typeof(MyClass).GetCustomAttributes(false);
                          ((MyAttribAttribute)a[0]).Value = "a1";
                  
                          Console.WriteLine("Getting attributes for MyClass.");
                          a = typeof(MyClass).GetCustomAttributes(false);
                          Console.WriteLine(((MyAttribAttribute)a[0]).Value);
                  
                          Console.ReadKey();
                      }
                  }
                  }
                  

                  现在如果 I 要实现属性,我希望输出是:

                  Now if I were to implement attributes, I would expect the output to be:

                  Created MyAttrib instance
                  Getting attributes for MyClass.
                  Getting attributes for MyClass.
                  a1
                  

                  类加载器"(对不起,我有更多的 Java 背景,不是 100% 确定 .net 如何加载其类型)将编译 MyClass,并创建 MyAttribAttribute 的实例,并将它们一起存储在某个地方.(如果这是 Java,可能是堆中的 Perm Gen)对 GetCustomAttributes() 的 2 次调用将返回相同的早期创建的实例.

                  Where the "class loader" (sorry, I have more of a Java background, not 100% sure how .net loads its types) would compile MyClass, and create an instance of MyAttribAttribute, and store them together somewhere. (probably the Perm Gen in the heap if this were Java) The 2 calls to GetCustomAttributes() would then just return the same earlier created instance.

                  但实际输出是:

                  Getting attributes for MyClass.
                  Created MyAttrib instance
                  Getting attributes for MyClass.
                  Created MyAttrib instance
                  SetOnClass
                  

                  那么……为什么?似乎为每次调用创建所有这些对象的新实例有点过分,并且不利于性能/内存管理.有什么方法可以一遍又一遍地获取相同的实例?

                  So... why? It seems like creating a new instance of all these objects for every call is a bit excessive, and not good for performance/memory management. Is there any way to always get the same instance over and over?

                  有人知道为什么要这样设计吗?

                  Anyone have any ideas why it was designed this way?

                  我完全关心的原因是因为我创建了一个内部保存一些验证信息的自定义属性,所以在属性中我基本上有一个private bool Validated",我将其设置为 true.验证的东西需要一段时间,所以我不想每次都运行它.现在的问题是,因为每次我获取属性时它都会创建一个新的属性实例,所以 Validated 总是假".

                  The reason I care at all is because I made a custom attribute that internally holds some validation information, so in the Attribute I basically have a "private bool Validated" that I set to true. The validation stuff takes a while, so I don't want to run it every time. Now the problem is that since it creates a new instance of the attribute each time I get the attributes, Validated is always "false".

                  推荐答案

                  对象创建很便宜.

                  如果你有类似的属性

                  public class MyAttribute : Attribute {
                      public virtual string MyText { get; set; }
                  }
                  

                  并将其应用于类似的类

                  [MyAttribute(MyText="some text")]
                  public class MyClass {
                  }
                  

                  你检索到一个喜欢

                  var attr =
                      typeof(MyClass).GetCustomAttributes(typeof(MyAttribute), false)
                      .Cast<MyAttribute>().Single();
                  

                  你在它上面设置了一些属性

                  and you set some properties on it like

                  attr.MyText = "not the text we started with";
                  

                  应该发生什么,以及发生什么,下次你打电话时

                  what should happen, and what would happen, the next time you called

                  Console.WriteLine(
                      typeof(MyClass).GetCustomAttributes(typeof(MyAttribute), false)
                      .Cast<MyAttribute>().Single().Name
                  );
                  

                  ?

                  这篇关于.NET 属性:为什么 GetCustomAttributes() 每次都创建一个新的属性实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .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 中的代码生成属性相关联)
                  1. <small id='YkD1v'></small><noframes id='YkD1v'>

                    <legend id='YkD1v'><style id='YkD1v'><dir id='YkD1v'><q id='YkD1v'></q></dir></style></legend>
                  2. <tfoot id='YkD1v'></tfoot>
                      <i id='YkD1v'><tr id='YkD1v'><dt id='YkD1v'><q id='YkD1v'><span id='YkD1v'><b id='YkD1v'><form id='YkD1v'><ins id='YkD1v'></ins><ul id='YkD1v'></ul><sub id='YkD1v'></sub></form><legend id='YkD1v'></legend><bdo id='YkD1v'><pre id='YkD1v'><center id='YkD1v'></center></pre></bdo></b><th id='YkD1v'></th></span></q></dt></tr></i><div id='YkD1v'><tfoot id='YkD1v'></tfoot><dl id='YkD1v'><fieldset id='YkD1v'></fieldset></dl></div>
                        <tbody id='YkD1v'></tbody>

                          <bdo id='YkD1v'></bdo><ul id='YkD1v'></ul>