1. <tfoot id='MNMqN'></tfoot>

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

  2. <legend id='MNMqN'><style id='MNMqN'><dir id='MNMqN'><q id='MNMqN'></q></dir></style></legend>

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

      • <bdo id='MNMqN'></bdo><ul id='MNMqN'></ul>

      什么时候创建属性实例?

      When are Attribute instances created?(什么时候创建属性实例?)
    1. <i id='Dg4Tc'><tr id='Dg4Tc'><dt id='Dg4Tc'><q id='Dg4Tc'><span id='Dg4Tc'><b id='Dg4Tc'><form id='Dg4Tc'><ins id='Dg4Tc'></ins><ul id='Dg4Tc'></ul><sub id='Dg4Tc'></sub></form><legend id='Dg4Tc'></legend><bdo id='Dg4Tc'><pre id='Dg4Tc'><center id='Dg4Tc'></center></pre></bdo></b><th id='Dg4Tc'></th></span></q></dt></tr></i><div id='Dg4Tc'><tfoot id='Dg4Tc'></tfoot><dl id='Dg4Tc'><fieldset id='Dg4Tc'></fieldset></dl></div>
    2. <small id='Dg4Tc'></small><noframes id='Dg4Tc'>

        <tbody id='Dg4Tc'></tbody>
      <tfoot id='Dg4Tc'></tfoot>

          <bdo id='Dg4Tc'></bdo><ul id='Dg4Tc'></ul>
          <legend id='Dg4Tc'><style id='Dg4Tc'><dir id='Dg4Tc'><q id='Dg4Tc'></q></dir></style></legend>

              1. 本文介绍了什么时候创建属性实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                属性是否与应用它们的类的实例同时实例化?

                Are attributes instantiated at the same time as instances of the class to which they are applied?

                推荐答案

                在调用 Type.GetCustomAttributes() 时会加载属性.如果只是实例化类,甚至获取类型,属性还没有加载.

                Attributes are loaded when the Type.GetCustomAttributes() is called. If you just instantiate the class or even get the type, the attributes are not yet loaded.

                看这个例子:

                class Program
                {
                    static void Main()
                    {
                        Console.WriteLine("Calling AttributeLoadTest.TestStatic():");
                        AttributeLoadTest.TestStatic();
                        Console.WriteLine("Loading class");
                        var x = new AttributeLoadTest();
                        Console.WriteLine("Loading type..");
                        var t = typeof (AttributeLoadTest);
                        Console.WriteLine("Calling GetCustomAttributes()");
                        var ats = t.GetCustomAttributes(false);
                    }
                }
                
                [ConsoleTest]
                class AttributeLoadTest
                {
                    public static void TestStatic()
                    {
                
                    }
                }
                
                [AttributeUsage(AttributeTargets.Class)]
                class ConsoleTestAttribute : Attribute
                {
                    public ConsoleTestAttribute()
                    {
                        Console.WriteLine("ConsoleTestAttribute Constructor");
                    }
                }
                

                打印出来:

                调用 AttributeLoadTest.TestStatic():

                Calling AttributeLoadTest.TestStatic():

                加载类

                加载类型..

                调用 GetCustomAttributes()

                Calling GetCustomAttributes()

                ConsoleTestAttribute 构造函数

                ConsoleTestAttribute Constructor

                所以调用静态方法,实例化类,获取类型,并没有实例化属性.调用 GetCustomAttributes() 可以.

                So calling a static method, instantiating the class, getting the type, does not instantiate the attribute. Calling GetCustomAttributes() does.

                这篇关于什么时候创建属性实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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 中的代码生成属性相关联)

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

                1. <tfoot id='aUZoX'></tfoot>
                    <bdo id='aUZoX'></bdo><ul id='aUZoX'></ul>
                    <legend id='aUZoX'><style id='aUZoX'><dir id='aUZoX'><q id='aUZoX'></q></dir></style></legend>
                      <tbody id='aUZoX'></tbody>

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