mercredi 22 juillet 2020

Why is reflection not showing my C# attribute [duplicate]

I have the following class:

 public class LookUpModel
    {

        [Key]
        public string Id { get; set; }

        [IsSearchable]
        public string Code{ get; set; }

        [IsSearchable]
        public string Name { get; set; }

        [IsSearchable]
        public string Address { get; set; }

        [IsSearchable]
        public string PostCode { get; set; }  
        

In order to make a comparison with some other data, I am trying to get the number of attributes back. I.e. I want to know that there are 4 'IsSearchable' attributes on this class. I have tried the following code:

                    var lookupModel= new LookUpModel() { Id = "1", Address = "", Name = "", PostCode = ""};
                    var attributes = gpModel.GetType().GetCustomAttributes(true).Where(a => a.GetType() == typeof(IsSearchableAttribute));
                    var attributeCount = attributes.Count();

However this returns 0? Can anyone please point me in the right direction here/point out where I am using reflection incorrectly? Thanks





Aucun commentaire:

Enregistrer un commentaire