Example #1
0
        public void Bind(NameValueCollection nameValueCollection)
        {
            BeforeBind(nameValueCollection);

            if (_propertiesCache == null)
            {
                _propertiesCache = new Dictionary <string, ParamProperty[]>();
            }

            var type = GetType();

            ParamProperty[] paramProperties;
            if (_propertiesCache.TryGetValue(type.FullName, out paramProperties))
            {
                foreach (ParamProperty paramProperty in paramProperties)
                {
                    var value = type.GetProperty(paramProperty.PropertyName).GetValue(this);
                    if (value != null)
                    {
                        nameValueCollection[paramProperty.ParamName] = value.ToString();
                    }
                }
            }
            else
            {
                var properties = type.GetProperties();
                paramProperties = new ParamProperty[properties.Length];

                int i = 0;
                foreach (var property in properties)
                {
                    var paramPropety = new ParamProperty
                    {
                        ParamName    = property.Name,
                        PropertyName = property.Name
                    };

                    var requestProperty = (RequestProperty)property.GetCustomAttribute(typeof(RequestProperty));
                    if (requestProperty != null)
                    {
                        if (requestProperty.Ignore)
                        {
                            continue;
                        }

                        paramPropety.ParamName = requestProperty.Name;
                    }

                    paramProperties[i++] = paramPropety;

                    var value = property.GetValue(this);
                    if (value != null)
                    {
                        nameValueCollection[paramPropety.ParamName] = value.ToString();
                    }
                }

                _propertiesCache.Add(type.FullName, paramProperties);
            }
        }
        public void EditablePart_NewObject_TheSameValue(bool editable,
                                                        int[] expectedValue)
        {
            object value        = 0;
            string name         = "Name";
            object defaultValue = null;

            property = new ParamProperty(name, value, defaultValue, editable);

            Assert.AreEqual(expectedValue, property.EditablePart);
        }
        public void IsEditable_NewObject_TheSameValue(bool actualValue,
                                                      bool expectedValue)
        {
            object value        = 0;
            string name         = "Name";
            object defaultValue = null;

            property =
                new ParamProperty(name, value, defaultValue, actualValue);

            Assert.AreEqual(expectedValue, property.IsEditable);
        }
        public void SetNewValue_NewObject_NewValueOrTheSame(object initValue,
                                                            object setValue, object expectedValue)
        {
            string name         = "Name";
            object defaultValue = null;
            bool   editable     = false; // Still allow value editing

            property = new ParamProperty(name, initValue, defaultValue,
                                         editable);
            property.SetNewValue(setValue.ToString());

            Assert.AreEqual(expectedValue.ToString(), property.Value);
        }