-2

I have different List of Custom class. and need to check for the properties of String type and validate the value of the property. if has some thing wrong value then change the value with some predefined value. For this I thought to create a Generic method of public IEnumerable validate (this IEnumerable ListofData)

but now I am unable to traverse on the properties and check the value and return the same after manipulating. Please find the sample code.

Thanks.

 public static  class ValidateExt
{
   public static IEnumerable<T> ValidateStringData<T>(this IEnumerable<T> DataList)
   {
       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
       foreach(T Item in DataList)
       {
           // here want to manipulate the date of those properties which is of string type and assign it back.
       }
   }
}
Santosh Jha
  • 13
  • 1
  • 7
  • Not to be rude, but I don't think that this is a question, sounds like a demand for us to do your research.. – demogorgon Aug 09 '17 at 04:12
  • 1
    Would suggest you post some of your code, or at least an example of how you want it to behave. – Evan Trimboli Aug 09 '17 at 04:13
  • Sorry, please find the some code which I Tried . public static class ValidateExt { public static IEnumerable ValidateStringData(this IEnumerable DataList) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); foreach(T Item in DataList) { // here want to manipulate the date of those properties which is of string type and assign it back. } } } – Santosh Jha Aug 09 '17 at 04:51
  • If it's for a specific class, what is it generic anyway? If I where in your place, I would add a method called `ValidateStringData` to your custom class, and do something like `myList.ForEach(cc => cc.ValidateStringData())`. – Zohar Peled Aug 09 '17 at 05:06
  • It's for different list of classes having lots of properties and need to change the value is it contains some predefined text. – Santosh Jha Aug 09 '17 at 05:36
  • This may be useful for you: https://stackoverflow.com/questions/824802/c-how-to-get-all-public-both-get-and-set-string-properties-of-a-type – Evan Trimboli Aug 09 '17 at 05:47
  • Ok, why do you think your extension method should belong to the IEnumerable? Does your classes implements a common interface? (It really should since you are working on specific properties.) If they do, why not add an extension method to that interface? – Zohar Peled Aug 09 '17 at 05:50

1 Answers1

2

Not sure how you are going to have different check functions for different string properties (or are you just using same logic for all string properties), but here is example how you can read all string properties and set them to be something else if some condition is met.

public static IEnumerable<T> ValidateStringData<T>(this IEnumerable<T> dataList)
{
    var properties = typeof(T).GetProperties().Where(p => p.PropertyType == typeof(string)).ToArray();
    foreach (var item in dataList)
    {
        foreach (var propertyInfo in properties)
        {
            var value = propertyInfo.GetValue(item, null) as string;
            string newValue;

            if (CheckString(value, out newValue))
            {
                propertyInfo.SetValue(item, newValue, null);
            }
        }

        yield return item;
    }
}

private static bool CheckString(string value, out string newValue)
{
    // do your validation logic here.
}
Janne Matikainen
  • 5,061
  • 15
  • 21