protected virtual object BindObject(Type targetType, Type objType, object obj) { var model = Activator.CreateInstance(targetType, true); // If the object type is a dictionary (we're using ExpandoObject instead of Dictionary now) // Then attempt to bind all the members if (typeof(IDictionary <string, object>).IsAssignableFrom(objType)) { var dictionary = (IDictionary <string, object>)obj; var members = BindingMemberInfo.Collect(targetType).ToList(); foreach (var modelProperty in members) { object val; if (dictionary.TryGetValue(modelProperty.Name, out val)) { var propertyVal = Bind(val, modelProperty.Type); modelProperty.SetValue(model, propertyVal); } } } return(model); }
/// <summary> /// Bind object. /// </summary> /// <param name="targetType">the target param type.</param> /// <param name="objType">Type of the object.</param> /// <param name="obj">object to be converted into a model.</param> /// <returns> /// An object. /// </returns> protected virtual object BindObject(Type targetType, Type objType, object obj) { var model = Activator.CreateInstance(targetType, true); // If the object type is a dictionary (we're using ExpandoObject instead of Dictionary now) // Then attempt to bind all the members if (typeof(IDictionary <string, object>).IsAssignableFrom(objType)) { var dictionary = (IDictionary <string, object>)obj; //TODO: Add Caching var members = BindingMemberInfo.Collect(targetType).ToList(); //TODO: We currently go through all the propertie/fields and //atempt to find a match in the dictionary, we should probably //do the reverse and go through all the keys in the dictionary //and see if there is a match to a property member foreach (var modelProperty in members) { object val; var propertyName = GetPropertyName(modelProperty); if (dictionary.TryGetValue(propertyName, out val)) { var propertyVal = Bind(val, modelProperty.Type); modelProperty.SetValue(model, propertyVal); } } } return(model); }
protected virtual IEnumerable <BindingMemberInfo> GetBindingMembers(Type modelType, Type genericType) { var blackListHash = new HashSet <string>(BlackListedPropertyNames, StringComparer.Ordinal); return(BindingMemberInfo.Collect(genericType ?? modelType).Where(member => !blackListHash.Contains(member.Name))); }