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

        <bdo id='YDmjR'></bdo><ul id='YDmjR'></ul>
      <tfoot id='YDmjR'></tfoot>

    2. <legend id='YDmjR'><style id='YDmjR'><dir id='YDmjR'><q id='YDmjR'></q></dir></style></legend>

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

        Attribute.IsDefined 看不到应用于 MetadataType 类的属性

        Attribute.IsDefined doesn#39;t see attributes applied with MetadataType class(Attribute.IsDefined 看不到应用于 MetadataType 类的属性)
        <tfoot id='cRwhT'></tfoot>

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

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

                • <small id='cRwhT'></small><noframes id='cRwhT'>

                  本文介绍了Attribute.IsDefined 看不到应用于 MetadataType 类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我通过 MetadataType 属性,通过 Attribute.IsDefined(). 有谁知道为什么,或者我做错了什么?

                  If I apply attributes to a partial class via the MetadataType attribute, those attributes are not found via Attribute.IsDefined(). Anyone know why, or what I'm doing wrong?

                  下面是我为此创建的一个测试项目,但我真的在尝试将自定义属性应用于 LINQ to SQL 实体类 - 例如 这个问题的答案.

                  Below is a test project I created for this, but I'm really trying to apply custom attributes to a LINQ to SQL entity class - like this answer in this question.

                  谢谢!

                  using System;
                  using System.ComponentModel.DataAnnotations;
                  using System.Reflection;
                  
                  namespace MetaDataTest
                  {
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              PropertyInfo[] properties = typeof(MyTestClass).GetProperties();
                  
                              foreach (PropertyInfo propertyInfo in properties)
                              {
                                  Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
                                  Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
                                  Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
                  
                                  // Displays:
                                  // False
                                  // False
                                  // 0
                              }
                  
                              Console.ReadLine();
                          }
                      }
                  
                      [MetadataType(typeof(MyMeta))]
                      public partial class MyTestClass
                      {
                          public string MyField { get; set; }
                      }
                  
                      public class MyMeta
                      {
                          [MyAttribute()]
                          public string MyField { get; set; }
                      }
                  
                      [AttributeUsage(AttributeTargets.All)]
                      public class MyAttribute : System.Attribute
                      {
                      }
                  }
                  

                  推荐答案

                  MetadataType 属性用于指定帮助指定数据对象的附加信息.要访问其他属性,您需要执行以下操作:

                  The MetadataType attribute is used to specify help specify the additional information on the data object. To access the additional attributes you would need to do something like the following:

                  using System;
                  using System.Linq;
                  using System.ComponentModel.DataAnnotations;
                  using System.Reflection;
                  
                  namespace MetaDataTest
                  {
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              MetadataTypeAttribute[] metadataTypes = typeof(MyTestClass).GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().ToArray();
                              MetadataTypeAttribute metadata = metadataTypes.FirstOrDefault();
                  
                              if (metadata != null)
                              {
                                  PropertyInfo[] properties = metadata.MetadataClassType.GetProperties();
                  
                                  foreach (PropertyInfo propertyInfo in properties)
                                  {
                                      Console.WriteLine(Attribute.IsDefined(propertyInfo, typeof(MyAttribute)));
                                      Console.WriteLine(propertyInfo.IsDefined(typeof(MyAttribute), true));
                                      Console.WriteLine(propertyInfo.GetCustomAttributes(true).Length);
                                      RequiredAttribute attrib = (RequiredAttribute)propertyInfo.GetCustomAttributes(typeof(RequiredAttribute), true)[0];
                                      Console.WriteLine(attrib.ErrorMessage);
                                  }
                  
                                  // Results:
                                  // True
                                  // True
                                  // 2
                                  // MyField is Required
                              }
                  
                              Console.ReadLine();
                          }
                      }
                  
                      [MetadataType(typeof(MyMeta))]
                      public partial class MyTestClass
                      {
                          public string MyField { get; set; }
                      }
                  
                      public class MyMeta
                      {
                          [MyAttribute()]
                          [Required(ErrorMessage="MyField is Required")]
                          public string MyField { get; set; }
                      }
                  
                      [AttributeUsage(AttributeTargets.All)]
                      public class MyAttribute : System.Attribute
                      {
                      }
                  }
                  

                  这还包括一个示例属性,用于显示如何提取添加的信息.

                  This also includes a sample attribute to show how to extract info that was added.

                  这篇关于Attribute.IsDefined 看不到应用于 MetadataType 类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的代码生成属性相关联)
                  <i id='WIUKn'><tr id='WIUKn'><dt id='WIUKn'><q id='WIUKn'><span id='WIUKn'><b id='WIUKn'><form id='WIUKn'><ins id='WIUKn'></ins><ul id='WIUKn'></ul><sub id='WIUKn'></sub></form><legend id='WIUKn'></legend><bdo id='WIUKn'><pre id='WIUKn'><center id='WIUKn'></center></pre></bdo></b><th id='WIUKn'></th></span></q></dt></tr></i><div id='WIUKn'><tfoot id='WIUKn'></tfoot><dl id='WIUKn'><fieldset id='WIUKn'></fieldset></dl></div>

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

                • <legend id='WIUKn'><style id='WIUKn'><dir id='WIUKn'><q id='WIUKn'></q></dir></style></legend>

                        <bdo id='WIUKn'></bdo><ul id='WIUKn'></ul>

                          <tbody id='WIUKn'></tbody>
                        1. <tfoot id='WIUKn'></tfoot>