lundi 11 juin 2018

Reflection is not giving count for Attribute wise properties

I am having class with list of properties with Column attribute and I am assigning value to other object as per below function. But it is coming 0 when I count with ColumnAttribute.

public static void PopulateModelWithNoNull(object sourceObj, object targetObj)
        {
            var sourceProperties = TypeDescriptor.GetProperties(sourceObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);

            var targetProperties = TypeDescriptor.GetProperties(targetObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);


            for (int i = 0; i < sourceProperties.Count(); i++)
            {
                if (sourceProperties.ElementAt(i).GetValue(sourceObj) != null)
                {
                    targetProperties.ElementAt(i).SetValue(targetObj,
                        sourceProperties.ElementAt(i).GetValue(sourceObj));
                }
            }
        }

If I count without ColumnAttribute, it is working fine and returns count. However, it is working fine in one another project solution. Any ideas?





Aucun commentaire:

Enregistrer un commentaire