dimanche 17 février 2019

MetadataType and Attribute.IsDefined

I am using a database first approach and have a class SalesWallboard mapped to the database table.

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

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.

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?





Aucun commentaire:

Enregistrer un commentaire