vendredi 5 octobre 2018

Check if attribute property is set without try-catch;

The System.ComponentModel.DataAnnotations.DisplayAttribute has properties (such as AutoGenerateField, AutoGenerateFilter, Order). If any of these attrs not set, when I attempt to read, fall into error.

var atProp = attribute.GetType().GetProperties();
foreach (var p in atProp)
{
   if (p.CanRead )
   {
       try
       {
           //var v = p.GetGetMethod().Invoke(at,null);
           var v = p.GetValue(at);
       }
       catch 
       {
           val = null;
       }

       if (val != null)
       {    
          // ...proccessing
       }
   }
}

How can I find out : Are attrs set or not without try-catch block?





Aucun commentaire:

Enregistrer un commentaire