<bdo id='eHdgQ'></bdo><ul id='eHdgQ'></ul>
    1. <tfoot id='eHdgQ'></tfoot>

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

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

        C# Buddy 类/元数据和反射

        时间:2023-07-28

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

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

                  <tbody id='gZmRA'></tbody>
                <legend id='gZmRA'><style id='gZmRA'><dir id='gZmRA'><q id='gZmRA'></q></dir></style></legend>
                1. <tfoot id='gZmRA'></tfoot>
                2. 本文介绍了C# Buddy 类/元数据和反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用反射来检查给定类的属性是否具有 ReadOnly 属性集.我使用的类是 MVC 视图模型(对元数据使用部分伙伴"类.

                  I am trying to use reflection to check if properties on a given class have a ReadOnly attribute set. The classes I am using are MVC View Models (using a partial "buddy" class for the metadata.

                   public partial class AccountViewModel  
                  {
                      public virtual Int32 ID { get; set; } 
                      public virtual decimal Balance { get; set; }    
                  
                  } 
                  [MetadataType(typeof(AccountViewModelMetaData))]
                  public partial class AccountViewModel  
                  {
                      class AccountViewModelMetaData
                      {
                          [DisplayName("ID")]
                          public virtual Int32 ID { get; set; }
                  
                          [DisplayName("Balance")]
                          [DataType(DataType.Currency)] 
                          [ReadOnly(true)]
                          public virtual decimal Balance { get; set; }
                  
                      }
                  }
                  

                  我想检查Balance"是否具有 ReadOnly 属性.如果我在 AccountViewModel 的 Balance 属性上设置 ReadOnly 属性,我可以这样检索它:

                  I want to check if "Balance" has the ReadOnly property. If i set the ReadOnly attribute on the Balance property of AccountViewModel, i can retrieve it this way:

                  Type t = typeof(AccountViewModel);
                  PropertyInfo pi = t.GetProperty("Balance");  
                  bool isReadOnly =  ReadOnlyAttribute.IsDefined(pi,typeof( ReadOnlyAttribute);
                  

                  如果属性信息在元数据类上,我无法检索它.如何检查属性是否存在?我为所有视图模型定义了元数据类,并且需要一种通用的方法来检查元数据类的属性.

                  I can't retrieve the attribute info if it is on the meta data class. How can i check if the attribute exists? I have meta data classes defined for all my view models, and need a generic way to check for attributes on meta data classes.

                  有什么建议吗?

                  推荐答案

                  解决方案是使用 GetCustomAttributes 获取 MetaData 类型并检查它们的属性...

                  the solution is to use GetCustomAttributes to get MetaData types and check the properties on those as well...

                  Type t = typeof(MyClass);
                  PropertyInfo pi = t.GetProperty(PropertyName);  
                  bool isReadOnly = ReadOnlyAttribute.IsDefined(pi, typeof(ReadOnlyAttribute));
                  
                  if (!isReadOnly)
                  {
                      //check for meta data class.
                      MetadataTypeAttribute[] metaAttr = (MetadataTypeAttribute[])t.GetCustomAttributes(typeof(MetadataTypeAttribute),true);
                  
                      if (metaAttr.Length > 0)
                      {
                          foreach (MetadataTypeAttribute attr in metaAttr)
                          {
                              t = attr.MetadataClassType;
                              pi = t.GetProperty(PropertyName);
                  
                              if (pi != null) isReadOnly = ReadOnlyAttribute.IsDefined(pi, typeof(ReadOnlyAttribute));
                  
                              if (isReadOnly) break;
                          }
                      }
                  } 
                  

                  这篇关于C# Buddy 类/元数据和反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Roslyn 检查属性是否用自定义属性装饰? 下一篇:覆盖属性设置器和获取器

                  相关文章

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

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

                    <tfoot id='ektZ4'></tfoot>