【问题标题】:MetadataType and Attribute.IsDefinedMetadataType 和 Attribute.IsDefined
【发布时间】:2023-03-10 03:32:01
【问题描述】:

我使用的是数据库优先方法,并将一个 SalesWallboard 类映射到数据库表。

SalesWallboard.cs:

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

我为此创建了一个 MetadataType 类来应用自定义属性

SalesWallboardModel.cs

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

    }

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

但在我的代码中,当我使用 Attribute.IsDefined 检查它时,它总是抛出 false。

代码

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

我已经检查了here 给出的答案。但我不明白这个例子。 有人可以帮我简化一下吗?

【问题讨论】:

  • 您正在检查错误类型的属性。您的自定义属性是在 SalesWallboardModel 类型的属性上定义的,而不是 SalesWallboard
  • 如果我正确理解您的意图(我可能完全错了),您需要检查 SalesWallboard 类上是否存在 MetadataType 属性。如果该属性存在,则从中获取元数据类类型对象。然后检查此元数据类类型的属性是否存在 UpdateFromEclipse 属性(这正是您链接的问题中接受的答案所做的。密切注意接受的答案中的代码。)
  • @thehennyy,如果我没有错,因为 SalesWallboardModel 是 SalesWallboard 的 MetadataType 类,我不应该使用 SalesWallboard 吗?因为,如果我将 var salesWallboardColumns = typeof(SalesWallboard).GetProperties(); 更改为 var salesWallboardColumns = typeof(SalesWallboardModel).GetProperties();,我的代码会在前面的行中中断,我正在为 SalesWallboard 中的属性设置值。
  • @PriyankPanchal,您反映的是 SalesWallboard 的属性(没有应用 UpdateFromEclipse 属性),而不是 SalesWallboardModel。 MetadataType 属性的存在根本不会改变这一事实。属性的存在不会神奇地使代码/反射表现不同..
  • 我不知道你想要什么和需要做什么。您的问题是关于 SalesWallboard 类和关联的 SalesWallboardModel 类。你现在想问的已经不是你的问题了,而且即将变成冗长的讨论。也许花点时间仔细考虑一下是个好主意,然后,如果您仍在为应用程序的某些方面苦苦挣扎,请提出另一个新问题,该问题清楚明确地解释了您遇到的实际问题...

标签: c# reflection custom-attributes metadatatype


【解决方案1】:

我要感谢 @elgonzo 和 @thehennyy 的 cmets 并通过反思纠正我的理解。

我找到了我正在寻找的东西 here。但我必须进行如下所示的一些修改。

我创建了一个方法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;
}

我还得到了here 的帮助,因为我的类型在不同的程序集中。

方法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;
}

然后像这样在我的代码中使用它

代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多