Attribute.IsDefined没有看到MetadataType类应用属性 [英] Attribute.IsDefined doesn't see attributes applied with MetadataType class
问题描述
如果我通过<一个应用属性的分部类href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute.aspx\">MetadataType属性,这些属性不通过<一个发现href=\"http://msdn.microsoft.com/en-us/library/system.attribute.isdefined%28VS.85%29.aspx\">Attribute.IsDefined().任何人都知道为什么,还是我在做什么错了?
下面是我为这个创建了一个测试项目,但我真的尝试应用自定义属性到LINQ到SQL实体类 - 像<一个href=\"http://stackoverflow.com/questions/393687/how-can-i-add-my-attributes-to-$c$c-generated-linq2sql-classes-properties/667119#667119\">this在回答这个问题。
谢谢!
使用系统;
使用System.ComponentModel.DataAnnotations;
使用的System.Reflection;命名空间MetaDataTest
{
类节目
{
静态无效的主要(字串[] args)
{
的PropertyInfo []属性= typeof运算(MyTestClass).GetProperties(); 的foreach(的PropertyInfo的PropertyInfo的属性)
{
Console.WriteLine(Attribute.IsDefined(的PropertyInfo的typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof运算(MyAttribute),真));
Console.WriteLine(propertyInfo.GetCustomAttributes(真)。长度); //显示:
//假
//假
// 0
} 到Console.ReadLine();
}
} [MetadataType(typeof运算(MyMeta))]
公共部分类MyTestClass
{
公共字符串MyField的{搞定;组; }
} 公共类MyMeta
{
[MyAttribute()]
公共字符串MyField的{搞定;组; }
} [AttributeUsage(AttributeTargets.All)
公共类MyAttribute:System.Attribute
{
}
}
解决方案
的 MetadataType
属性用于指定
使用系统;
使用System.Linq的;
使用System.ComponentModel.DataAnnotations;
使用的System.Reflection;命名空间MetaDataTest
{
类节目
{
静态无效的主要(字串[] args)
{
MetadataTypeAttribute [] = metadataTypes typeof运算(MyTestClass).GetCustomAttributes(typeof运算(MetadataTypeAttribute),真).OfType&LT; MetadataTypeAttribute方式&gt;()ToArray的();
MetadataTypeAttribute中继= metadataTypes.FirstOrDefault();如果(元数据!= NULL)
{
的PropertyInfo []属性= metadata.MetadataClassType.GetProperties();的foreach(的PropertyInfo的PropertyInfo的属性)
{
Console.WriteLine(Attribute.IsDefined(的PropertyInfo的typeof(MyAttribute)));
Console.WriteLine(propertyInfo.IsDefined(typeof运算(MyAttribute),真));
Console.WriteLine(propertyInfo.GetCustomAttributes(真)。长度);
ATTRIB RequiredAttribute标签=(RequiredAttribute标签)propertyInfo.GetCustomAttributes(typeof运算(RequiredAttribute标签),真)[0];
Console.WriteLine(attrib.ErrorMessage);
}//结果:
//真
//真
// 2
// MyField的是必需的
} 到Console.ReadLine();
}
} [MetadataType(typeof运算(MyMeta))]
公共部分类MyTestClass
{
公共字符串MyField的{搞定;组; }
} 公共类MyMeta
{
[MyAttribute()]
[必需(的ErrorMessage =MyField的是要求)]
公共字符串MyField的{搞定;组; }
} [AttributeUsage(AttributeTargets.All)
公共类MyAttribute:System.Attribute
{
}
}
这还包括一个样本的属性,以显示如何提取加入该信息。
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?
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.
Thanks!
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
{
}
}
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类应用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!