jeudi 26 septembre 2019

How can I get field value via reflection if name is different with field name

I have a class

 [BsonIgnoreExtraElements]
    public class CustomerModel
    {        
        public string BranchID { get; set; }       
        public string BranchName { get; set; }        
        public string ReceiverID { get; set; }
        public string ReceiverName{ get; set; }       

        }

I am writing a filter activity which can validate any field with specific value configured in MongoDB

"Exclude":[{"SourceCol":"Receiver Mode ID","Values":{"Value":["G","8","J"]}}

and written the comparing logic as

 public static object GetPropValue(object src, string propName)
        {
            return src.GetType().GetProperty(propName).GetValue(src, null);
        }

        public static bool CheckPropertyCompare(CustomerModel customer, Exclude item)
        {
            var propertyValue = GetPropValue(customer, item.SourceCol);
            return item.Values.Value.ToList().Contains(propertyValue);

        }

In this, the Receiver Mode ID from MongoDB is actually looking for ReceiverID and I am stuck as to how can I resolve this issue. The only option I can think of is Key-Value pair collection to fetch the field name. but would like to know if there is any options like Attributes which can ease this process.

TIA





Aucun commentaire:

Enregistrer un commentaire