public static ValidationResult Validate(this Common.BaseClassSerializable obj) { var type = Type.GetType( string.Format("FatturaElettronica.Validators.{0}Validator", obj.GetType().Name)); var instance = Activator.CreateInstance(type); var method = type.GetRuntimeMethod("Validate", new[] { obj.GetType() }); return((ValidationResult)method.Invoke(instance, new [] { obj })); }
/// <summary> /// Helper method to get a named property /// </summary> /// <param name="value"></param> /// <param name="name"></param> /// <returns></returns> private static PropertyInfo GetPropertyInfo(BaseClassSerializable value, string name) { var type = value.GetType(); var properties = value.GetAllDataProperties().ToList(); // XmlElementAttribute comes first var property = properties .Where(prop => prop.GetCustomAttributes(typeof(XmlElementAttribute), false) .Where(ca => ((XmlElementAttribute)ca).ElementName.Equals(name, StringComparison.OrdinalIgnoreCase)) .Any()) .FirstOrDefault(); // Fallback to property name if (property == null) { property = properties.FirstOrDefault(n => n.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } return(property); }