/// <summary>
        ///     Initializes a new instance of the <see cref="ConfigurationDictionaryBase{TKey, TElement}" /> class.
        /// </summary>
        /// <param name="parent">
        ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
        /// </param>
        /// <param name="propertyDef">
        ///     The definition of the property defined by this object.
        /// </param>
        /// <param name="items">
        ///     The elements with which to initialize to the collection.
        /// </param>
        /// <param name="configurationRoot">
        ///     The configuration root service from which values are read or to which all values will be written.
        /// </param>
        /// <param name="settings">
        ///     The settings used to control how configuration objects are created and the features they support.
        /// </param>
        protected ConfigurationDictionaryBase(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot,
                                              IEnumerable <KeyValuePair <TKey, TElement> >?items, ConfigurationObjectSettings settings) : this(parent, propertyDef, configurationRoot, settings)
        {
            PropertyDef = propertyDef;

            if (!ReferenceEquals(items, null))
            {
                foreach (var item in items)
                {
                    Element <TKey, TElement> element;
                    _disableReadOnly.Value = _disableReadOnly.Value + 1;
                    try
                    {
                        element = new Element <TKey, TElement>(propertyDef, this, item.Key)
                        {
                            Value = item.Value
                        };
                    }
                    finally
                    {
                        _disableReadOnly.Value = _disableReadOnly.Value - 1;
                    }

                    _itemsByKey.Add(item.Key, element);
                    _orderedItems.Add(element);
                    element.Saved();
                }
            }
        }
Exemplo n.º 2
0
        private static bool WriteStringElement(IScope scope, IWriter writer, IPropertyDef property, XName name, object value)
        {
            string s;

            if (!scope.TryConvert(value, out s))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(s))
            {
                return(true);
            }

            if (property.Type == typeof(object))
            {
                writer.WriteObjectElement(name, value);
            }
            else
            {
                writer.WritePrimitiveElement(name, s);
            }

            return(true);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="ValueBase{TParent, TValue}" /> class.
        /// </summary>
        /// <param name="propertyDef">
        ///     The definition of the property to represent.
        /// </param>
        /// <param name="parent">
        ///     The parent configuration object for which this object represents a property.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="parent" /> is <see langword="null" />.
        /// </exception>
        internal ValueBase(IPropertyDef propertyDef, TParent parent)
        {
            parent.Validate(nameof(parent), ObjectIs.NotNull);

            _parent      = parent;
            _propertyDef = propertyDef;
            _isSaved     = false;
        }
Exemplo n.º 4
0
        private static void CreateProperty(TypeBuilder typeBuilder, IPropertyDef pdef, FieldBuilder isdirty)
        {
            var propertyName = pdef.Name;
            var propType     = pdef.PropertyType.Type;

            //Define the field and the property
            var field    = typeBuilder.DefineField("_" + propertyName, propType, FieldAttributes.Private);
            var property = typeBuilder.DefineProperty(propertyName,
                                                      System.Reflection.PropertyAttributes.None,
                                                      propType,
                                                      new[] { propType });

            const MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.HideBySig;

            // Define the "get" and "set" accessor methods
            var currGetPropMthdBldr = typeBuilder.DefineMethod("get_" + propertyName,
                                                               getSetAttr,
                                                               propType,
                                                               Type.EmptyTypes);

            var currGetIl = currGetPropMthdBldr.GetILGenerator();

            currGetIl.Emit(OpCodes.Ldarg_0);
            currGetIl.Emit(OpCodes.Ldfld, field);
            currGetIl.Emit(OpCodes.Ret);

            var currSetPropMthdBldr = typeBuilder.DefineMethod("set_" + propertyName,
                                                               getSetAttr,
                                                               null,
                                                               new[] { propType });

            //store value in private field and set the isdirty flag
            var currSetIl = currSetPropMthdBldr.GetILGenerator();

            currSetIl.Emit(OpCodes.Ldarg_0);
            currSetIl.Emit(OpCodes.Ldarg_1);
            currSetIl.Emit(OpCodes.Stfld, field);
            currSetIl.Emit(OpCodes.Ldarg_0);
            currSetIl.Emit(OpCodes.Ldc_I4_1);
            currSetIl.Emit(OpCodes.Stfld, isdirty);
            //currSetIl.Emit(OpCodes.Call, setIsDirtyMethod);
            currSetIl.Emit(OpCodes.Ret);


            //var keyAttribute = typeof(KeyAttribute);
            //var myConstructorInfo = keyAttribute.GetConstructor(new Type[] { });
            //var attributeBuilder = new CustomAttributeBuilder(myConstructorInfo, new object[] { });
            //property.SetCustomAttribute(attributeBuilder);


            property.SetGetMethod(currGetPropMthdBldr);
            property.SetSetMethod(currSetPropMthdBldr);

            //var getMethod = typeof(T).GetMethod("get_" + propertyName);
            //var setMethod = typeof(T).GetMethod("set_" + propertyName);
            //typeBuilder.DefineMethodOverride(currGetPropMthdBldr, getMethod);
            //typeBuilder.DefineMethodOverride(currSetPropMthdBldr, setMethod);
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="ConfigurationDictionaryBase{TKey, TElement}" /> class.
        /// </summary>
        /// <param name="parent">
        ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
        /// </param>
        /// <param name="propertyDef">
        ///     The definition of the property defined by this object.
        /// </param>
        /// <param name="configurationRoot">
        ///     The configuration root service from which values are read or to which all values will be written.
        /// </param>
        /// <param name="settings">
        ///     The settings used to control how configuration objects are created and the features they support.
        /// </param>
        protected ConfigurationDictionaryBase(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot, ConfigurationObjectSettings settings)
        {
            _parent           = parent;
            PropertyDef       = propertyDef;
            ConfigurationRoot = configurationRoot;
            _settings         = settings;

            RegisterReloadToken();
        }
Exemplo n.º 6
0
        private static object CreateElement(IPropertyDef def, object target)
        {
            if (def == null)
            {
                throw new NotSupportedException();
            }
            var element = target != null?def.GetValue(target) : null;

            return(element ?? Activator.CreateInstance(def.Type));
        }
Exemplo n.º 7
0
        private static bool WriteStringElement(IScope scope, IWriter writer, IPropertyDef property, XName name, object value)
        {
            string s;
            if (!scope.TryConvert(value, out s))
                return false;

            if (string.IsNullOrEmpty(s))
                return true;

            if (property.Type == typeof(object))
                writer.WriteObjectElement(name, value);
            else
                writer.WritePrimitiveElement(name, s);

            return true;
        }
            public PropertyCollection Add <TValue>(Expression <Func <T, TValue> > property, Func <TValue, bool> isDefaultValue, params XName[] names)
            {
                IPropertyDef prop = null;

                if (names != null && names.Length > 0)
                {
                    foreach (var name in names)
                    {
                        if (prop == null)
                        {
                            prop = Create(property, isDefaultValue, null, name);
                            _properties.Add(name, prop);
                        }
                        else
                        {
                            _properties.Alias(name, new PropertyFork(prop, name));
                        }
                    }
                }
                else
                {
                    foreach (var ns in _namespaces)
                    {
                        if (prop == null)
                        {
                            prop = Create(property, isDefaultValue, ns, null);
                            var name = ns + prop.Name.LocalName;
                            _properties.Add(name, prop);
                        }
                        else
                        {
                            var name = ns + prop.Name.LocalName;
                            _properties.Alias(name, new PropertyFork(prop, name));
                        }
                    }
                }
                return(this);
            }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConfigurationDictionary{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     The elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ConfigurationDictionary(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot,
                                IEnumerable <KeyValuePair <string, TElement> >?elements, ConfigurationObjectSettings settings) : base(parent, propertyDef, configurationRoot, elements, settings)
 {
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConfigurationCollection{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     A parameter array containing the elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ConfigurationCollection(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot, ConfigurationObjectSettings settings,
                                params TElement[]?elements) : base(parent, propertyDef, configurationRoot, GetIndexedElements(elements), settings)
 {
 }
Exemplo n.º 11
0
        private static bool ReadValue(IScope scope, IReader reader, object obj, IElementDef def, IPropertyDef property, out object value)
        {
            var type = property.Type;

            if (type == typeof(object))
            {
                value = reader.ReadObject();
                return true;
            }

            if (scope.TryRead(() => reader.ReadString(), type, out value))
                return true;

            var xmlReader = reader as XmlReaderImpl;
            if (xmlReader != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    value = CreateElement(property, obj);
                    surrogate.Read(xmlReader.XmlReader, value);
                    return true;
                }
            }

            var elementDef = scope.GetElementDef(type);
            if (elementDef != null)
            {
                var subScope = elementDef as IScope;
                value = ReadElement(subScope ?? scope, reader, elementDef, () => CreateElement(property, obj));
                return true;
            }

            if (ReadEnumElement(reader, type, out value))
                return true;

            // try read collection
            var elementType = Reflector.GetItemType(type);
            if (elementType != null)
            {
                elementDef = new CollectionDef(scope, property.Name, type, elementType);
                value = def.IsImmutable ? CreateList(elementType) : CreateElement(property, obj);
                ReadElement(scope, reader, elementDef, value);
                return true;
            }

            value = null;
            return false;
        }
Exemplo n.º 12
0
        private static bool ReadValue(IScope scope, IReader reader, object obj, IElementDef def, IPropertyDef property, out object value)
        {
            var type = property.Type;

            if (type == typeof(object))
            {
                value = reader.ReadObject();
                return(true);
            }

            if (scope.TryRead(() => reader.ReadString(), type, out value))
            {
                return(true);
            }

            var xmlReader = reader as XmlReaderImpl;

            if (xmlReader != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    value = CreateElement(property, obj);
                    surrogate.Read(xmlReader.XmlReader, value);
                    return(true);
                }
            }

            var elementDef = scope.GetElementDef(type);

            if (elementDef != null)
            {
                var subScope = elementDef as IScope;
                value = ReadElement(subScope ?? scope, reader, elementDef, () => CreateElement(property, obj));
                return(true);
            }

            if (ReadEnumElement(reader, type, out value))
            {
                return(true);
            }

            // try read collection
            var elementType = Reflector.GetItemType(type);

            if (elementType != null)
            {
                elementDef = new CollectionDef(scope, property.Name, type, elementType);
                value      = def.IsImmutable ? CreateList(elementType) : CreateElement(property, obj);
                ReadElement(scope, reader, elementDef, value);
                return(true);
            }

            value = null;
            return(false);
        }
Exemplo n.º 13
0
        private static void WriteValue(IScope scope, IWriter writer, IPropertyDef property, XName name, object value)
        {
            if (value == null) return;

            if (value is Enum && WriteStringElement(scope, writer, property, name, value))
                return;

            if (value.IsPrimitive())
            {
                if (property.Type == typeof(object))
                {
                    writer.WriteObjectElement(name, value);
                }
                else
                {
                    writer.WritePrimitiveElement(name, value);
                }
                return;
            }

            if (WriteStringElement(scope, writer, property, name, value))
                return;

            var type = value.GetType();
            var xmlWriter = writer as XmlWriterImpl;
            if (xmlWriter != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    surrogate.Write(xmlWriter.XmlWriter, value);
                    return;
                }
            }

            var elementDef = scope.GetElementDef(type);
            if (elementDef != null)
            {
                var subScope = elementDef as IScope;
                WriteElement(subScope, writer, value, elementDef, elementDef.Name);
                return;
            }

            var collection = value as IEnumerable;
            if (collection != null)
            {
                var itemDef = new CollectionItemDef(property.ItemName, Reflector.GetItemType(type));
                var empty = true;
                foreach (var item in collection)
                {
                    if (empty)
                    {
                        writer.WriteStartCollection(name);
                        empty = false;
                    }
                    if (item == null)
                    {
                        writer.WriteNullItem(property.ItemName);
                        continue;
                    }
                    WriteValue(scope, writer, itemDef, property.ItemName, item);
                }
                if (!empty) writer.WriteEndCollection();
                return;
            }

            throw new InvalidOperationException(string.Format("Unknown element. Name: {0}. Type: {1}.", name, type));
        }
 public PropertyFork(IPropertyDef property, XName name)
 {
     _property = property;
     Name      = name;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="ReadOnlyConfigurationCollection{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     The elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ReadOnlyConfigurationCollection(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot,
                                        IEnumerable <TElement>?elements, ConfigurationObjectSettings settings) : base(parent, propertyDef, configurationRoot, GetIndexedElements(elements), settings)
 {
 }
Exemplo n.º 16
0
 private static object CreateElement(IPropertyDef def, object target)
 {
     if (def == null) throw new NotSupportedException();
     var element = target != null ? def.GetValue(target) : null;
     return element ?? Activator.CreateInstance(def.Type);
 }
Exemplo n.º 17
0
        private static void WriteValue(IScope scope, IWriter writer, IPropertyDef property, XName name, object value)
        {
            if (value == null)
            {
                return;
            }

            if (value is Enum && WriteStringElement(scope, writer, property, name, value))
            {
                return;
            }

            if (value.IsPrimitive())
            {
                if (property.Type == typeof(object))
                {
                    writer.WriteObjectElement(name, value);
                }
                else
                {
                    writer.WritePrimitiveElement(name, value);
                }
                return;
            }

            if (WriteStringElement(scope, writer, property, name, value))
            {
                return;
            }

            var type      = value.GetType();
            var xmlWriter = writer as XmlWriterImpl;

            if (xmlWriter != null)
            {
                var surrogate = scope.GetSurrogate(type);
                if (surrogate != null)
                {
                    surrogate.Write(xmlWriter.XmlWriter, value);
                    return;
                }
            }

            var elementDef = scope.GetElementDef(type);

            if (elementDef != null)
            {
                var subScope = elementDef as IScope;
                WriteElement(subScope, writer, value, elementDef, elementDef.Name);
                return;
            }

            var collection = value as IEnumerable;

            if (collection != null)
            {
                var itemDef = new CollectionItemDef(property.ItemName, Reflector.GetItemType(type));
                var empty   = true;
                foreach (var item in collection)
                {
                    if (empty)
                    {
                        writer.WriteStartCollection(name);
                        empty = false;
                    }
                    if (item == null)
                    {
                        writer.WriteNullItem(property.ItemName);
                        continue;
                    }
                    WriteValue(scope, writer, itemDef, property.ItemName, item);
                }
                if (!empty)
                {
                    writer.WriteEndCollection();
                }
                return;
            }

            throw new InvalidOperationException(string.Format("Unknown element. Name: {0}. Type: {1}.", name, type));
        }
 public ConfigurationObjectMock(IPropertyDef propertyDef, IConfigurationRoot configurationRoot, IConfigurationParent parent, ConfigurationObjectSettings settings) : base(propertyDef, configurationRoot, parent, settings, System.Array.Empty <IConfigurationObjectValidator <IRootElement> >())
 {
 }
Exemplo n.º 19
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="ConfigurationDictionary{TElement}" /> class.
 /// </summary>
 /// <param name="parent">
 ///     The parent object to which this one belongs. <see langword="null" /> if this is a root object.
 /// </param>
 /// <param name="propertyDef">
 ///     The definition of the property defined by this object.
 /// </param>
 /// <param name="configurationRoot">
 ///     The configuration root service from which values are read or to which all values will be written.
 /// </param>
 /// <param name="elements">
 ///     A parameter array containing the elements with which to initialize to the collection.
 /// </param>
 /// <param name="settings">
 ///     The settings used to control how configuration objects are created and the features they support.
 /// </param>
 public ReadOnlyConfigurationDictionary(IConfigurationParent?parent, IPropertyDef propertyDef, IConfigurationRoot configurationRoot, ConfigurationObjectSettings settings,
                                        params KeyValuePair <string, TElement>[]?elements) : base(parent, propertyDef, configurationRoot, elements, settings)
 {
 }
Exemplo n.º 20
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Element{TKey,TValue}" /> class.
 /// </summary>
 /// <param name="propertyDef"> The definition of the property to represent. </param>
 /// <param name="parent"> The parent configuration dictionary for which this object represents a property. </param>
 /// <param name="key"> The key that identifies this element in the collection. </param>
 internal Element(IPropertyDef propertyDef, ConfigurationDictionaryBase <TKey, TValue> parent, TKey key) : base(propertyDef, parent)
 {
     Key = key;
 }