示例#1
0
        private static void BindCollection(object collection, Type collectionType, DynamicElement config)
        {
            var typeInfo = collectionType.GetTypeInfo();

            // ICollection<T> is guaranteed to have exacly one parameter
            var itemType  = typeInfo.GenericTypeArguments[0];
            var addMethod = typeInfo.GetDeclaredMethod("Add");

            foreach (var section in config.GetChildren2())
            {
                try
                {
                    var item = BindInstance(
                        type: itemType,
                        instance: null,
                        element: section);
                    if (item != null)
                    {
                        addMethod.Invoke(collection, new[] { item });
                    }
                }
                catch
                {
                }
            }
        }
示例#2
0
        private static Array BindArray(Array source, DynamicElement config)
        {
            var children    = config.GetChildren2().ToArray();
            var arrayLength = source.Length;
            var elementType = source.GetType().GetElementType();
            var newArray    = Array.CreateInstance(elementType, arrayLength + children.Length);

            // binding to array has to preserve already initialized arrays with values
            if (arrayLength > 0)
            {
                Array.Copy(source, newArray, arrayLength);
            }

            for (int i = 0; i < children.Length; i++)
            {
                try
                {
                    var item = BindInstance(
                        type: elementType,
                        instance: null,
                        element: children[i]);
                    if (item != null)
                    {
                        newArray.SetValue(item, arrayLength + i);
                    }
                }
                catch
                {
                }
            }

            return(newArray);
        }
示例#3
0
        private static void BindDictionary(object dictionary, Type dictionaryType, DynamicElement config)
        {
            var typeInfo = dictionaryType.GetTypeInfo();

            // IDictionary<K,V> is guaranteed to have exactly two parameters
            var keyType   = typeInfo.GenericTypeArguments[0];
            var valueType = typeInfo.GenericTypeArguments[1];

            if (keyType != typeof(string))
            {
                // We only support string keys
                return;
            }

            var addMethod = typeInfo.GetDeclaredMethod("Add");

            foreach (var child in config.GetChildren2())
            {
                var item = BindInstance(
                    type: valueType,
                    instance: null,
                    element: child);
                if (item != null)
                {
                    //var key = child.Key;
                    var key = string.Empty; //TODO
                    addMethod.Invoke(dictionary, new[] { key, item });
                }
            }
        }