mardi 5 avril 2016

reflection trim all strings in list with list c#

I have a method with trim all strings in first level.

public static IEnumerable<T> Trim<T>(this IEnumerable<T> collection)
{
    foreach (var item in collection)
    {
        var stringProperties = item.GetType().GetProperties()
                  .Where(p => p.PropertyType == typeof(string));

        foreach (var stringProperty in stringProperties)
        {
            var currentValue = (string)stringProperty.GetValue(item, null);

            if (currentValue != null)
                stringProperty.SetValue(item, currentValue.Trim(), null);
        }
    }
    return collection;
}

But if my property is a List I need apply trim in all string properties in this list, someone help me?





Aucun commentaire:

Enregistrer un commentaire