Example #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 Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()));
            }
        }
 public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
 {
     ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
     if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <, >), out Type dictionaryDefinition))
     {
         Type dictionaryKeyType   = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition);
         Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition);
         Func <Type, IList <object>, object> instanceCreator = delegate(Type t, IList <object> a)
         {
             ConstructorInfo constructor = t.GetConstructor(new Type[1]
             {
                 dictionaryDefinition
             });
             return(constructor.Invoke(new object[1]
             {
                 dictionary
             }));
         };
         return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <, >), new Type[2]
         {
             dictionaryKeyType,
             dictionaryValueType
         }, instanceCreator, dictionary));
     }
     if (dictionary is IDictionary)
     {
         return(new DictionaryWrapper <object, object>((IDictionary)dictionary));
     }
     throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()));
 }
Example #3
0
        public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
        {
            ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
            Type dictionaryDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out dictionaryDefinition))
            {
                return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <,>), (IList <Type>) new Type[2]
                {
                    ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition),
                    ReflectionUtils.GetDictionaryValueType(dictionaryDefinition)
                }, (Func <Type, IList <object>, object>)((t, a) => t.GetConstructor(new Type[1]
                {
                    dictionaryDefinition
                }).Invoke(new object[1]
                {
                    dictionary
                })), new object[1]
                {
                    dictionary
                }));
            }
            else if (dictionary is IDictionary)
            {
                return((IWrappedDictionary) new DictionaryWrapper <object, object>((IDictionary)dictionary));
            }
            else
            {
                throw new ArgumentException(StringUtils.FormatWith("Can not create DictionaryWrapper for type {0}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)dictionary.GetType()), "dictionary");
            }
        }
        public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
        {
            Type type;

            ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
            if (!ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out type))
            {
                if (!(dictionary is IDictionary))
                {
                    throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[] { dictionary.GetType() }));
                }
                return(new DictionaryWrapper <object, object>((IDictionary)dictionary));
            }
            Type dictionaryKeyType   = ReflectionUtils.GetDictionaryKeyType(type);
            Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(type);
            Func <Type, IList <object>, object> func = (Type t, IList <object> a) => t.GetConstructor(new Type[] { type }).Invoke(new object[] { dictionary });
            Type type1 = typeof(DictionaryWrapper <,>);

            Type[]   typeArray = new Type[] { dictionaryKeyType, dictionaryValueType };
            object[] objArray  = new object[] { dictionary };
            return((IWrappedDictionary)ReflectionUtils.CreateGeneric(type1, (IList <Type>)typeArray, func, objArray));
        }