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

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

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

        C# 反射:在成员字段上查找属性

        C# Reflection : Finding Attributes on a Member Field(C# 反射:在成员字段上查找属性)
      2. <i id='2uVxm'><tr id='2uVxm'><dt id='2uVxm'><q id='2uVxm'><span id='2uVxm'><b id='2uVxm'><form id='2uVxm'><ins id='2uVxm'></ins><ul id='2uVxm'></ul><sub id='2uVxm'></sub></form><legend id='2uVxm'></legend><bdo id='2uVxm'><pre id='2uVxm'><center id='2uVxm'></center></pre></bdo></b><th id='2uVxm'></th></span></q></dt></tr></i><div id='2uVxm'><tfoot id='2uVxm'></tfoot><dl id='2uVxm'><fieldset id='2uVxm'></fieldset></dl></div>

            <small id='2uVxm'></small><noframes id='2uVxm'>

              <bdo id='2uVxm'></bdo><ul id='2uVxm'></ul>
                  <tbody id='2uVxm'></tbody>

                1. <tfoot id='2uVxm'></tfoot>
                  <legend id='2uVxm'><style id='2uVxm'><dir id='2uVxm'><q id='2uVxm'></q></dir></style></legend>
                2. 本文介绍了C# 反射:在成员字段上查找属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我可能问错了这个问题,但是您可以/如何在其自身中找到某个类的字段...例如...

                  I may be asking this incorrectly, but can/how can you find fields on a class within itself... for example...

                  public class HtmlPart {
                    public void Render() {
                      //this.GetType().GetCustomAttributes(typeof(OptionalAttribute), false);
                    }
                  }
                  
                  public class HtmlForm {
                    private HtmlPart _FirstPart = new HtmlPart();      
                    [Optional] //<-- how do I find that?
                    private HtmlPart _SecondPart = new HtmlPart();
                  }
                  

                  或者也许我只是做错了......我怎样才能调用一个方法,然后检查应用于自身的属性?

                  Or maybe I'm just doing this incorrectly... How can I call a method and then check for attributes applied to itself?

                  另外,为了这个问题 - 我只是好奇是否可以在不知道/访问父类的情况下找到属性信息

                  Also, for the sake of the question - I'm just curious if it was possible to find attribute information without knowing/accessing the parent class!

                  推荐答案

                  如果我正确理解你的问题,我认为你试图做的事情是不可能的......

                  If I understand your question correctly, I think what you are trying to do is not possible...

                  Render 方法中,您希望获得应用于对象的可能属性.该属性属于 _SecondPart 字段,而该属性属于 HtmlForm 类.

                  In the Render method, you want to get a possible attribute applied to the object. The attribute belongs to the field _SecondPart witch belongs to the class HtmlForm.

                  为此,您必须将调用对象传递给 Render 方法:

                  For that to work you would have to pass the calling object to the Render method:

                      public class HtmlPart {
                          public void Render(object obj) {
                              FieldInfo[] infos = obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                  
                              foreach (var fi in infos)
                              {
                                  if (fi.GetValue(obj) == this && fi.IsDefined(typeof(OptionalAttribute), true))
                                      Console.WriteLine("Optional is Defined");
                              }
                          }
                      }
                  

                  这篇关于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 中的代码生成属性相关联)
                      <legend id='5NPlk'><style id='5NPlk'><dir id='5NPlk'><q id='5NPlk'></q></dir></style></legend>

                      <small id='5NPlk'></small><noframes id='5NPlk'>

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

                            <tfoot id='5NPlk'></tfoot>