• <bdo id='lZT1M'></bdo><ul id='lZT1M'></ul>
  1. <legend id='lZT1M'><style id='lZT1M'><dir id='lZT1M'><q id='lZT1M'></q></dir></style></legend>
    <tfoot id='lZT1M'></tfoot>

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

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

      C# 覆盖子类中的属性

      时间:2023-07-27

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

          <bdo id='omQ8j'></bdo><ul id='omQ8j'></ul>
          • <tfoot id='omQ8j'></tfoot>
              <tbody id='omQ8j'></tbody>

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

                本文介绍了C# 覆盖子类中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                public class MyWebControl {
                
                    [ExternallyVisible]
                    public string StyleString {get;set;}
                
                }
                
                public class SmarterWebControl : MyWebControl {
                
                    [ExternallyVisible]
                    public string CssName{get;set;}
                
                    new public string StyleString {get;set;} //Doesn't work
                
                }
                

                是否可以删除子类中的属性?我确实希望该属性被其他子类继承,而不是这个.

                Is it possible to remove the attribute in the subclass? I do want the attribute to get inherited by other subclasses, just not this one.

                哎呀,好像我忘了编译什么的,因为上面发布的代码确实有效!

                Whoops, looks like I forgot to compile or something because the code as posted above does, in fact, work!

                推荐答案

                它对我有用.

                测试代码:

                public static void Main()
                {
                    var attribute = GetAttribute(typeof (MyWebControl), "StyleString", false);
                    Debug.Assert(attribute != null);
                
                    attribute = GetAttribute(typeof(SmarterWebControl), "StyleString", false);
                    Debug.Assert(attribute == null);
                
                    attribute = GetAttribute(typeof(SmarterWebControl), "StyleString", true);
                    Debug.Assert(attribute == null);
                }
                
                private static ExternallyVisibleAttribute GetAttribute(Type type, string propertyName, bool inherit)
                {
                    PropertyInfo property = type.GetProperties().Where(p=>p.Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                
                    var list = property.GetCustomAttributes(typeof(ExternallyVisibleAttribute), inherit).Select(o => (ExternallyVisibleAttribute)o);
                
                    return list.FirstOrDefault();
                }
                

                这篇关于C# 覆盖子类中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:通过反射获取 MemberInfo 的类型 下一篇:使用泛型的 xUnit 理论测试

                相关文章

                  <bdo id='7mrr5'></bdo><ul id='7mrr5'></ul>
              1. <legend id='7mrr5'><style id='7mrr5'><dir id='7mrr5'><q id='7mrr5'></q></dir></style></legend>
                <tfoot id='7mrr5'></tfoot>

                    <small id='7mrr5'></small><noframes id='7mrr5'>

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