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.

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

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

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                Func Delegate vs Function(函数委托与函数)
                CreateDelegate with unknown types(具有未知类型的 CreateDelegate)

                <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>