예제 #1
0
        /// <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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        protected virtual void BindValue(BindingMemberInfo modelProperty, object obj, BindingContext context)
        {
            if (obj == null)
            {
                return;
            }

            Type dictionaryType = typeof(Dictionary <string, object>);

            //If the type is a dictionary and the PropertyType isn't then we'll bind.
            if (obj.GetType() == dictionaryType && modelProperty.PropertyType != dictionaryType)
            {
                //We have a sub dictionary, attempt to bind it to the class
                var model = Bind(obj, modelProperty.PropertyType);

                modelProperty.SetValue(context.Model, model);
            }
            //If both types are collections then we'll bind
            else if (obj.GetType().IsCollectionOrArray() && modelProperty.PropertyType.IsCollectionOrArray())
            {
                //We have a sub dictionary, attempt to bind it to the class
                var model = Bind(obj, modelProperty.PropertyType);

                modelProperty.SetValue(context.Model, model);
            }
            else
            {
                //Simply set the property
                modelProperty.SetValue(context.Model, obj);
            }
        }
예제 #4
0
 private string GetPropertyName(BindingMemberInfo modelProperty)
 {
     if (javascriptNameConverter == null)
     {
         return(modelProperty.Name);
     }
     return(javascriptNameConverter.ConvertReturnedObjectPropertyAndFieldToNameJavascript(modelProperty));
 }
예제 #5
0
        /// <summary>
        /// Compares two BindingMemberInfo's with eachother on their respective values rather then their reference
        /// </summary>
        /// <param name="obj">the other BindingMemberInfo</param>
        /// <returns>true when they are equal and false otherwise</returns>
        public bool Equals(BindingMemberInfo obj)
        {
            if (obj == null)
            {
                return(false);
            }

            return(this.MemberInfo.Equals(obj.MemberInfo));
        }
예제 #6
0
        protected virtual void BindValue(BindingMemberInfo modelProperty, object obj, BindingContext context)
        {
            if (obj == null)
            {
                return;
            }

            if (modelProperty.PropertyType.IsAssignableFrom(obj.GetType()))
            {
                // Simply set the property
                modelProperty.SetValue(context.Model, obj);
            }
            else
            {
                // Cannot directly set the property attempt to bind
                var model = Bind(obj, modelProperty.PropertyType);

                modelProperty.SetValue(context.Model, model);
            }
        }
예제 #7
0
        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)));
        }