Exemplo n.º 1
0
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            bool ignoreIsNull     = IgnoreValue == null;
            bool defaultIsNull    = DefaultValue == null;
            bool configItemIsNull = ConfigManager[Key] == null;

            //Prevent Default = Ignore
            if (!ignoreIsNull && !defaultIsNull && IgnoreValue.Equals(DefaultValue))
            {
                throw new Exception("IgnoreValue must not be equal DefaultValue");
            }
            // If ConfigurationItem does not exist, Set to Default
            if (configItemIsNull)
            {
                ConfigManager[Key] = new ConfigurationItem(Key, DefaultValue);
            }
            // If Ignore != null, ConfigurationItem Exists, and ConfigurationItem Value == Ignore, Set to Default
            else if (!ignoreIsNull && ConfigManager[Key].Value.Equals(IgnoreValue))
            {
                ConfigManager[Key] = new ConfigurationItem(Key, DefaultValue);
            }

            Binding binding = new Binding
            {
                Source = ConfigManager,
                Path   = new PropertyPath(string.Format("[{0}].Value", Key)),
                Mode   = BindingMode.TwoWay,
            };

            return(binding.ProvideValue(serviceProvider));
        }
        public virtual string GetPropertyValue(PropertyInfo property, object obj)
        {
            object propertyValue = property.GetValue(obj);

            string propertyString = propertyValue.ToString();

            if (!string.IsNullOrEmpty(FormatString))
            {
                propertyString = string.Format("{0:" + FormatString + "}", propertyValue);
            }
            if (IgnoreValue != null && IgnoreValue.Equals(propertyValue))
            {
                return(string.Empty);
            }
            if (Uppercase)
            {
                propertyString = propertyString.ToUpper();
            }
            return(propertyString);
        }