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

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

  2. <tfoot id='XsbyP'></tfoot>

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

      通过反射获取 MemberInfo 的类型

      Getting the type of a MemberInfo with reflection(通过反射获取 MemberInfo 的类型)
            <bdo id='Ttl0R'></bdo><ul id='Ttl0R'></ul>

          • <legend id='Ttl0R'><style id='Ttl0R'><dir id='Ttl0R'><q id='Ttl0R'></q></dir></style></legend>
              <tbody id='Ttl0R'></tbody>

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

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

              <tfoot id='Ttl0R'></tfoot>

              • 本文介绍了通过反射获取 MemberInfo 的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用反射来加载具有项目类结构的树视图.类中的每个成员都有一个分配给他们的自定义属性.

                I'm using reflection to load a treeview with the class structure of a project. Each of the members in a class have a custom attribute assigned to them.

                使用 MemberInfo.GetCustomAttributes() 获取类的属性没有问题,但是我需要一种方法来确定类成员是否是自定义类,然后需要对其自身进行解析返回自定义属性.

                I don't have a problem getting the attributes for a class using MemberInfo.GetCustomAttributes() however I need a way of working out if a class member is a custom class and then needs parsing itself to return the custom attributes.

                到目前为止,我的代码是:

                So far, my code is:

                MemberInfo[] membersInfo = typeof(Project).GetProperties();
                
                foreach (MemberInfo memberInfo in membersInfo)
                {
                    foreach (object attribute in memberInfo.GetCustomAttributes(true))
                    {
                        // Get the custom attribute of the class and store on the treeview
                        if (attribute is ReportAttribute)
                        {
                            if (((ReportAttribute)attribute).FriendlyName.Length > 0)
                            {
                               treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName });
                            }
                        }
                        // PROBLEM HERE : I need to work out if the object is a specific type
                        //                and then use reflection to get the structure and attributes.
                    }
                }
                

                是否有一种简单的方法可以获取 MemberInfo 实例的目标类型,以便我可以适当地处理它?我觉得我遗漏了一些明显的东西,但我现在正在绕圈子.

                Is there an easy way of getting the target type of a MemberInfo instance so I can handle it appropriately? I feel I'm missing something obvious but I'm going round in circles at the minute.

                推荐答案

                GetProperties 返回一个 PropertyInfo 数组,所以你应该使用它.
                那么使用 PropertyType 属性.

                GetProperties returns an array of PropertyInfo so you should use that.
                Then it is simply a matter of using the PropertyType property.

                PropertyInfo[] propertyInfos = typeof(Project).GetProperties();
                
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    // ...
                    if(propertyInfo.PropertyType == typeof(MyCustomClass))
                        // ...
                }
                

                这篇关于通过反射获取 MemberInfo 的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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='kc9A3'></bdo><ul id='kc9A3'></ul>

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

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