public void TryPopulateModel(object model, IParameterCollection parameterCollection) { if (parameterCollection == null) { throw new ArgumentNullException("parameterCollection"); } var errors = new Dictionary <string, Exception>(); ICachedType type = PropertyProvider.Get(model.GetType()); foreach (IParameter parameter in parameterCollection) { try { object value = parameter.Value; type.SetConvertedValue(model, parameter.Name, value); } catch (Exception err) { errors[parameter.Name] = err; } } if (errors.Count != 0) { throw new PropertyException(errors); } }
/// <summary> /// Gets all methods attributed with <paramref name="attribute"/>. /// </summary> /// <remarks> /// This includes instance methods and static methods. It <b>excludes</b> constructors. /// </remarks> /// <param name="type">The type to find methods within.</param> /// <param name="attribute">The attribute to look for.</param> /// <returns>A list of cached method information which may be empty.</returns> public List <ICachedMethod> GetAttributedMethods(Type type, Type attribute) { CacheType(type); List <ICachedMethod> methods = new List <ICachedMethod>(); ICachedType cachedType = typeCache[type]; foreach (string key in cachedType.Methods.Keys) { methods.AddRange(cachedType.Methods[key].FindAll(m => m.HasAttribute(attribute))); } return(methods); }
/// <summary> /// Assign properties in the specified object. /// </summary> /// <param name="instance">Object to fill.</param> /// <param name="parameters">Contains all parameters that should be assigned to the properties.</param> /// <exception cref="PropertyException">Properties was not found or value could not be converted.</exception> /// <exception cref="ArgumentNullException">Any parameter is <c>null</c>.</exception> public static void Assign(object instance, IParameterCollection parameters) { if (instance == null) { throw new ArgumentNullException("instance"); } if (parameters == null) { throw new ArgumentNullException("parameters"); } var errors = new Dictionary <string, Exception>(); ICachedType type = PropertyProvider.Get(instance.GetType()); foreach (IParameter parameter in parameters) { try { object value = parameter.Value; if (_handler != null && !_handler(instance, parameter.Name, ref value)) { continue; } type.SetConvertedValue(instance, parameter.Name, value); } catch (Exception err) { errors[parameter.Name] = err; } } if (errors.Count != 0) { throw new PropertyException(errors); } }