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

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

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

        你能在 C# 中获得一个类的合并属性吗?

        Can you get merged attributes for a class in C#?(你能在 C# 中获得一个类的合并属性吗?)
        <legend id='SBKsf'><style id='SBKsf'><dir id='SBKsf'><q id='SBKsf'></q></dir></style></legend>
          <bdo id='SBKsf'></bdo><ul id='SBKsf'></ul>

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

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

                  <tbody id='SBKsf'></tbody>

                1. 本文介绍了你能在 C# 中获得一个类的合并属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有以下一组类,是否可以合并 DerivedClass 的属性?目前,如果我使用传递 true 的 GetType().GetCustomAttributes() 方法进行继承,它将采用继承结构中的最高属性.

                  Say I have the following set of classes, is it possible for the attributes of DerivedClass to be merged? Currently if I use the GetType().GetCustomAttributes()method passing in true for inheritance it takes the highest attributes in the inheritance structure.

                  即[另一个(鲍勃")]和[我的(16)]

                  i.e. [Another("Bob")] and [My(16)]

                  属性可以合并吗?所以我最终会得到两个属性 [My(16, "Male")] 和 [Another("Bob")]

                  Is it possible for the attributes to be merged? So I would end up with two attributes of [My(16, "Male")] and [Another("Bob")]

                  我并不是说我会指定一个属性为 [My(16, "Male")],而是我会返回一个年龄为 16 岁且性别为男性的属性.

                  I don't mean to say that I would specify an attribute of [My(16, "Male")] but rather I would be returned an attribute with an Age of 16 and a Gender of Male.

                  public class MyAttribute : Attribute
                  {
                  
                      public MyAttribute(int age)
                      {
                          Age = age;
                      }
                  
                      public MyAttribute(int age, string gender)
                      {
                          Age = age;
                          Gender = gender;
                      }
                  
                      public int Age { get; set; }
                  
                      public string Gender { get; set; }
                  
                  }
                  
                  public class AnotherAttribute : Attribute
                  {
                  
                      public AnotherAttribute(string name)
                      {
                          Name = name;
                      }
                  
                      public string Name { get; set; }
                  
                  }
                  
                  [My(12, "Male")]
                  [Another("Bob")]
                  public class BaseClass
                  {
                  }
                  
                  [My(16)]
                  public class DerivedClass : BaseClass
                  {
                  }
                  

                  推荐答案

                  您可以在一个实体上拥有同一属性的多个实例(这是应用于您的属性的 AttributeUsageAttribute 上的一个选项).当你获取一个类型的属性时,你可以获取应用到继承链的所有属性.

                  You can have multiple instances of the same attribute on an entity (this is an option on the AttributeUsageAttribute applied to your attribute). When you get attributes on a type, you can get all the attributes applied to the inheritance chain.

                  但是,标准工具中没有任何东西会采用两个属性实例并生成一个.

                  However there is nothing in the standard tools that will take two attribute instances and make one.

                  (理论上,重写 MSIL 的后处理器可以做到这一点.)

                  (In theory a post processor that re-wrote the MSIL could do this.)

                  这篇关于你能在 C# 中获得一个类的合并属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

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

                      <bdo id='zVZWA'></bdo><ul id='zVZWA'></ul>
                      • <small id='zVZWA'></small><noframes id='zVZWA'>

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

                            <tfoot id='zVZWA'></tfoot>