vendredi 7 août 2020

Using MetadataType to add a property attribute shows 0 CustomAttributes at runtime

I'm hoping that I am doing something silly here. I am trying to put together a proof of concept for using MetadataType to add an attribute to a partial class (auto generated edmx file). here is my simplified code:

class Program
{
    static void Main(string[] args)
    {
        var a = typeof(Test).GetProperties();
        var prop = a.First();
        var atts = prop.CustomAttributes.Count();
    }

    /// <summary>
    /// mock of an edmx partial class
    /// </summary>
    public partial class Test
    {
        public string MyProperty { get; set; }
        public string AnotherProperty { get; set; }
    }

    /*         *
     * Another partial class with an additional attribute being linked
     */

    [MetadataType(typeof(TestMetaData))]
    public partial class Test { }
    public partial class TestMetaData
    {
        [Required(ErrorMessage = "Title is required.")]
        public object MyProperty { get; set; }
    }
}

When I run this the atts variable returns 0 custom attributes when I would've thought it should return the single "Required" attribute.

Note, that I know this bit of code is pointless in the real world, I am trying to show the issue as simply as I can.

Any advice greatly appreciated





Aucun commentaire:

Enregistrer un commentaire