private void Compare(object o, object p) { Dictionary <Type, DelegateConverter> converters = new Dictionary <Type, DelegateConverter>(); converters.Add(typeof(int), ChangeInt); converters.Add(typeof(double), ChangeDouble); converters.Add(typeof(float), ChangeSingle); converters.Add(typeof(long), ChangeLong); converters.Add(typeof(bool), ChangeBool); converters.Add(typeof(string), ChangeString); Dictionary <string, object> valueMap = new Dictionary <string, object>(); if (o != null) { foreach (PropertyInfo prop in o.GetType().GetProperties()) { // Recurse to get parameters. if (prop.CanRead) { object[] customAttributes = prop.GetCustomAttributes(typeof(ParameterFileAttribute), true); object potential = null; if (customAttributes.Length > 0) { potential = prop.GetValue(o, BindingFlags.GetProperty, null, null, null); } for (int i = 0; i < customAttributes.Length; i++) { ParameterFileAttribute attr = customAttributes[i] as ParameterFileAttribute; if (potential != null && attr != null && attr.Name != "") { valueMap.Add(attr.Name, potential); } else { throw new NullReferenceException("The parameter value cannot be null."); } } } } } else { throw new NullReferenceException("The object o should not be null."); } if (p != null) { foreach (PropertyInfo prop in p.GetType().GetProperties()) { // Recurse to get parameters. if (prop.CanRead) { object[] customAttributes = prop.GetCustomAttributes(typeof(ParameterFileAttribute), true); object potential = null; if (customAttributes.Length > 0) { potential = prop.GetValue(p, BindingFlags.GetProperty, null, null, null); } for (int i = 0; i < customAttributes.Length; i++) { ParameterFileAttribute attr = customAttributes[i] as ParameterFileAttribute; if (potential != null && attr != null && attr.Name != "") { object ovalue = valueMap[attr.Name]; bool isEqual = ovalue.ToString() == potential.ToString(); if (!isEqual) { throw new Exception("the two values should be equal. " + ovalue.ToString() + " " + potential.ToString()); } } else { throw new NullReferenceException("The parameter value cannot be null."); } } } } } else { throw new NullReferenceException("The object p should not be null."); } }
private object ChangeObjectValues(object o) { Dictionary <Type, DelegateConverter> converters = new Dictionary <Type, DelegateConverter>(); converters.Add(typeof(int), ChangeInt); converters.Add(typeof(double), ChangeDouble); converters.Add(typeof(float), ChangeSingle); converters.Add(typeof(long), ChangeLong); converters.Add(typeof(bool), ChangeBool); converters.Add(typeof(string), ChangeString); if (o != null) { foreach (PropertyInfo prop in o.GetType().GetProperties()) { // Recurse to get parameters. if (prop.CanRead) { object[] customAttributes = prop.GetCustomAttributes(typeof(ParameterFileAttribute), true); object potential = null; if (customAttributes.Length > 0) { potential = prop.GetValue(o, BindingFlags.GetProperty, null, null, null); } for (int i = 0; i < customAttributes.Length; i++) { ParameterFileAttribute attr = customAttributes[i] as ParameterFileAttribute; if (potential != null && attr != null && attr.Name != "") { Type data = potential.GetType(); // We want the key not found exception to be thrown! if (data.IsEnum) { potential = ChangeEnum(potential); } else { potential = converters[data](potential); } prop.SetValue(o, potential, BindingFlags.SetProperty, null, null, null); } else { throw new NullReferenceException("The parameter value cannot be null."); } } } } foreach (FieldInfo field in o.GetType().GetFields()) { object[] customAttributes = field.GetCustomAttributes(typeof(ParameterFileAttribute), true); object objectValue = null; if (customAttributes.Length > 0) { objectValue = field.GetValue(o); } for (int i = 0; i < customAttributes.Length; i++) { ParameterFileAttribute attr = customAttributes[i] as ParameterFileAttribute; if (objectValue != null && attr != null) { throw new Exception("There should be no fields"); } } } } return(o); }