• <legend id='DkVbA'><style id='DkVbA'><dir id='DkVbA'><q id='DkVbA'></q></dir></style></legend>

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

      1. <tfoot id='DkVbA'></tfoot>
      2. <small id='DkVbA'></small><noframes id='DkVbA'>

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

        获取描述属性在类级别

        时间:2023-07-27

        1. <small id='ztBmp'></small><noframes id='ztBmp'>

                <tbody id='ztBmp'></tbody>
              • <bdo id='ztBmp'></bdo><ul id='ztBmp'></ul>
                <legend id='ztBmp'><style id='ztBmp'><dir id='ztBmp'><q id='ztBmp'></q></dir></style></legend>

                <tfoot id='ztBmp'></tfoot>
                <i id='ztBmp'><tr id='ztBmp'><dt id='ztBmp'><q id='ztBmp'><span id='ztBmp'><b id='ztBmp'><form id='ztBmp'><ins id='ztBmp'></ins><ul id='ztBmp'></ul><sub id='ztBmp'></sub></form><legend id='ztBmp'></legend><bdo id='ztBmp'><pre id='ztBmp'><center id='ztBmp'></center></pre></bdo></b><th id='ztBmp'></th></span></q></dt></tr></i><div id='ztBmp'><tfoot id='ztBmp'></tfoot><dl id='ztBmp'><fieldset id='ztBmp'></fieldset></dl></div>
                  本文介绍了获取描述属性在类级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这样的课

                  [Description("This is a wahala class")]
                  public class Wahala
                  {
                  
                  }
                  

                  有没有办法获取Wahala类的Description属性的内容?

                  Is there anyway to get the content of the Description attribute for the Wahala class?

                  推荐答案

                  绝对 - 使用 Type.GetCustomAttributes.示例代码:

                  Absolutely - use Type.GetCustomAttributes. Sample code:

                  using System;
                  using System.ComponentModel;
                  
                  [Description("This is a wahala class")]
                  public class Wahala
                  {    
                  }
                  
                  public class Test
                  {
                      static void Main()
                      {
                          Console.WriteLine(GetDescription(typeof(Wahala)));
                      }
                  
                      static string GetDescription(Type type)
                      {
                          var descriptions = (DescriptionAttribute[])
                              type.GetCustomAttributes(typeof(DescriptionAttribute), false);
                  
                          if (descriptions.Length == 0)
                          {
                              return null;
                          }
                          return descriptions[0].Description;
                      }
                  }
                  

                  同种代码可以检索其他成员的描述,例如字段、属性等.

                  The same kind of code can retrieve descriptions for other members, such as fields, properties etc.

                  这篇关于获取描述属性在类级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 C# 中访问属性或 const 的描述属性? 下一篇:NonSerialized 和 Xml.Serialization.XmlIgnore 之间的区别?

                  相关文章

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

                    <tfoot id='qRhPV'></tfoot>

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

                    <legend id='qRhPV'><style id='qRhPV'><dir id='qRhPV'><q id='qRhPV'></q></dir></style></legend>

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