Jer*_*ins 3 c# asp.net reflection attributes .net-4.0
我有一个名为Project的LINQ生成类.
我想为生成的属性添加一些属性,因此我使用MetadataType属性定义了一个部分类.
[MetadataType(typeof(Project_HiddenProps))]
public partial class Project : IProject
{
// There are also a few additional properties defined here.
}
public class Project_HiddenProps
{
[HiddenColumn]
public System.Guid Id { get; set; }
// Id is an auto-generated property that I've added a custom attribute to
}
Run Code Online (Sandbox Code Playgroud)
后来,我使用反射来尝试获取此属性的属性.
var customAttributes = prop.GetCustomAttributes(false);
Run Code Online (Sandbox Code Playgroud)
但是,我只获得了一个属性类型System.Data.Linq.Mapping.ColumnAttribute.我的HiddenColumn属性未包含在集合中.我的问题是如何使用反射获取这些元数据属性.
任何帮助将不胜感激.
您可以使用约定方法反映其他类:
var type = Type.GetType(entityType.FullName + "_HiddenProps," + entityType.AssemblyQualifiedName);
type.GetProperty("Id").GetCustomAttributes(false);
Run Code Online (Sandbox Code Playgroud)
伙伴类不能自动与核心组件合并.那只用于内部框架元数据(假设这是MVC,或者可能是动态数据?)
正如@CyanLite在评论中提到的,您可以使用此处描述的元数据属性中的Meta类(Cyan添加的链接).