Ejemplo n.º 1
0
        public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
        {
            ValidationUtils.ArgumentNotNull(dictionary, "dictionary");

            Type dictionaryDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out dictionaryDefinition))
            {
                Type dictionaryKeyType   = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition);
                Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition);

                // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor
                Func <Type, IList <object>, object> instanceCreator = (t, a) =>
                {
                    ConstructorInfo c = t.GetConstructor(new[] { dictionaryDefinition });
                    return(c.Invoke(new[] { dictionary }));
                };

                return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <,>), new[] { dictionaryKeyType, dictionaryValueType }, instanceCreator, dictionary));
            }
            else if (dictionary is IDictionary)
            {
                return(new DictionaryWrapper <object, object>((IDictionary)dictionary));
            }
            else
            {
                throw new ArgumentException("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()), "dictionary");
            }
        }
Ejemplo n.º 2
0
        public static IWrappedCollection CreateCollectionWrapper(object list)
        {
            ValidationUtils.ArgumentNotNull(list, "list");

            Type collectionDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(ICollection <>), out collectionDefinition))
            {
                Type collectionItemType = ReflectionUtils.GetCollectionItemType(collectionDefinition);

                // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor
                Func <Type, IList <object>, object> instanceCreator = (t, a) =>
                {
                    ConstructorInfo c = t.GetConstructor(new[] { collectionDefinition });
                    return(c.Invoke(new[] { list }));
                };

                return((IWrappedCollection)ReflectionUtils.CreateGeneric(typeof(CollectionWrapper <>), new[] { collectionItemType }, instanceCreator, list));
            }
            else if (list is IList)
            {
                return(new CollectionWrapper <object>((IList)list));
            }
            else
            {
                throw new ArgumentException("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, list.GetType()), "list");
            }
        }
Ejemplo n.º 3
0
        public static IList CreateGenericList(Type listType)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");

            return((IList)ReflectionUtils.CreateGeneric(typeof(List <>), listType));
        }