Subclass validation not working or collection of subclass validation not working

Feb 18, 2015 at 11:37 AM
For those who are struck here,I am providing the solution.
I created one method inside ValidatingParameterInspector.cs.
  private object[] ValidateCollection(string operationName, object[] inputs)
        {
            object[] inputs1 = inputs;
            try
            {
                foreach (var input in inputs)
                {
                    foreach (var property in input.GetType().GetProperties().Where(prop => prop.PropertyType.IsClass || prop.PropertyType.Name.Contains("List")))
                    {
                        if (property.PropertyType == typeof(string) || property.PropertyType.IsValueType) continue;

                        IEnumerable enumerable = null;
                        if (property.PropertyType.Name.Contains("List"))
                        {
                            enumerable = property.GetValue(input, null) as IEnumerable;
                            if (enumerable == null) continue;

                            int j = 0;
                            object[] o1 = new object[inputs.Count() + enumerable.OfType<object>().Count()];
                            for (int k = 0; k < inputs.Count(); k++)
                            {
                                o1[k] = inputs[k];
                            }
                            foreach (var item in enumerable)
                            {

                                o1[inputs.Count() + j] = item;
                                j = j + 1;
                                if (j == (o1.Length - inputs.Count()))
                                    inputs = o1;
                            }
                        }
                        else if (property.PropertyType.IsClass)
                        {


                            var enumerable1 = property.GetValue(input, null);
                            if (enumerable1 == null) continue;
                            // enumerable = property.GetValue(input, null) as IEnumerable;
                            //enumerable = enumerable1 as IEnumerable;
                            int j = 0;
                            object[] o1 = new object[inputs.Count() + 1];
                            for (int k = 0; k < inputs.Count(); k++)
                            {
                                o1[k] = inputs[k];
                            }
                            o1[inputs.Count()] = enumerable1;
                            inputs = o1;

                        }

                    }
                }
                return inputs;
            }
            catch
            {
                return inputs1;
            }

        }
Then in BeforeCall method in same class,I call this like this,
public object BeforeCall(string operationName, object[] inputs)
        {
            var validationResults = new List<ValidationResult>();
           
           // I called it here notice.
            inputs = ValidateCollection(operationName, inputs);


            foreach (var input in inputs)
            {
                
                foreach (var validator in _validators)
                {
                    
                    var results = validator.Validate(input);
                    validationResults.AddRange(results);
                }
            }
            
            if (validationResults.Count > 0)
            {
               throw new FaultException(_errorMessageGenerator.GenerateErrorMessage(operationName, validationResults));
            
                
            }

            return null;
        }
Example
public Class Test1
{

   public Test2 _test2 {get;set;}
  or 
   public List<Test2>  _test2List  {get;set;}
}
public Class Test2
{
 define member here
}
Here both _test2 and _test2List will be validated.

Most importantly if there is any alternative nicer solution then let me know .