Esempio n. 1
0
        public object Bind(object obj)
        {
            var type = obj.GetType();

            var iProgramTool = ToolFactory.CreateIProgramTool();

            foreach (var propertyPath in this._NameValueCollection.AllKeys)
            {
                if (string.IsNullOrEmpty(propertyPath))
                {
                    continue;
                }

                var nvValues = this._NameValueCollection.GetValues(propertyPath);

                if (nvValues == null || nvValues.Length == 0)
                {
                    continue;
                }

                var bindingFlags = BindingFlags.IgnoreCase | BindingFlags.Instance |
                                   BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty;

                var property = ReflectorHelper.GetPropertyInfoOrDefault(type, propertyPath, bindingFlags);

                if (property == null)
                {
                    continue;
                }

                object realValue    = null;
                var    propertyType = property.PropertyType;

                try
                {
                    realValue = iProgramTool.ChanageType(nvValues, property.PropertyType);
                }
                catch
                {
                }

                if (realValue != null)
                {
                    ReflectorHelper.SetProperty(obj, propertyPath, realValue, null);
                }
            }
            return(obj);
        }