简体   繁体   English

MetadataType和Attribute.IsDefined

[英]MetadataType and Attribute.IsDefined

I am using a database first approach and have a class SalesWallboard mapped to the database table. 我正在使用数据库优先方法,并且有一个类SalesWallboard映射到数据库表。

SalesWallboard.cs: SalesWallboard.cs:

namespace Wallboards.DAL.SchnellDb
{
    public partial class SalesWallboard
    {
        public int Id { get; set; }
        public string StaffName { get; set; }
        ...
    }
}

I have created a MetadataType class for this to apply custom attributes 我为此创建了一个MetadataType类以应用自定义属性

SalesWallboardModel.cs SalesWallboardModel.cs

namespace Wallboards.DAL.SchnellDb
{
    [MetadataType (typeof(SalesWallboardModel))]
    public partial class SalesWallboard
    {

    }

    public class SalesWallboardModel
    {
        [UpdateFromEclipse]
        public string StaffName { get; set; }
        ...
    }
}

But in my code, when I check it using Attribute.IsDefined, it always throws false. 但是在我的代码中,当我使用Attribute.IsDefined检查它时,它总是抛出false。

Code

var salesWallboardColumns = typeof(SalesWallboard).GetProperties();
foreach (var column in salesWallboardColumns)
{
    if (Attribute.IsDefined(column, typeof(UpdateFromEclipse))) //returns false
    {
        ...
    }
}

I have checked the answer given here . 我已经检查了这里给出的答案。 But I don't understand the example. 但是我不明白这个例子。 Can someone please simplify this for me? 有人可以为我简化一下吗?

I would like to thank @elgonzo and @thehennyy for their comments and correcting my understanding with reflection. 我要感谢@elgonzo和@thehennyy的评论,并通过反思来纠正我的理解。

I found what I was looking for here . 我在这里找到了想要的东西 But I had to make a few modifications shown below. 但是我必须进行如下所示的一些修改。

I created a method PropertyHasAttribute 我创建了一个方法PropertyHasAttribute

private static bool PropertyHasAttribute<T>(string propertyName, Type attributeType)
{
    MetadataTypeAttribute att = (MetadataTypeAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(MetadataTypeAttribute));
    if (att != null)
    {
        foreach (var prop in GetType(att.MetadataClassType.UnderlyingSystemType.FullName).GetProperties())
        {
            if (propertyName.ToLower() == prop.Name.ToLower() && Attribute.IsDefined(prop, attributeType))
                return true;
        }
    }
    return false;
}

I also got help from here because my type was in a different assembly. 我也从这里得到了帮助,因为我的类型在不同的程序集中。

Method GetType 方法GetType

public static Type GetType(string typeName)
{
    var type = Type.GetType(typeName);
    if (type != null) return type;
    foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
    {
        type = a.GetType(typeName);
        if (type != null)
            return type;
    }
    return null;
}

Then used it in my code like so 然后像这样在我的代码中使用它

Code

var salesWallboardColumns = typeof(SalesWallboard).GetProperties();
foreach (var column in salesWallboardColumns)
{
    var columnName = column.Name;
    if (PropertyHasAttribute<SalesWallboard>(columnName, typeof(UpdateFromEclipse)))
    {
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM