public static object TranslateToConvertibleGenericICollectionCache(
            object from, Type toInstanceOfType, Type fromElementType)
        {
            var typeKey = new ConvertibleTypeKey(toInstanceOfType, fromElementType);
            ConvertInstanceDelegate translateToFn;

            if (TranslateConvertibleICollectionCache.TryGetValue(typeKey, out translateToFn))
            {
                return(translateToFn(from, toInstanceOfType));
            }

            var toElementType = toInstanceOfType.GetGenericType().GenericTypeArguments()[0];
            var genericType   = typeof(TranslateListWithConvertibleElements <,>).MakeGenericType(fromElementType, toElementType);
            var mi            = genericType.GetPublicStaticMethod("LateBoundTranslateToGenericICollection");

            translateToFn = (ConvertInstanceDelegate)mi.MakeDelegate(typeof(ConvertInstanceDelegate));

            Dictionary <ConvertibleTypeKey, ConvertInstanceDelegate> snapshot, newCache;

            do
            {
                snapshot          = TranslateConvertibleICollectionCache;
                newCache          = new Dictionary <ConvertibleTypeKey, ConvertInstanceDelegate>(TranslateConvertibleICollectionCache);
                newCache[typeKey] = translateToFn;
            } while (!ReferenceEquals(
                         Interlocked.CompareExchange(ref TranslateConvertibleICollectionCache, newCache, snapshot), snapshot));

            return(translateToFn(from, toInstanceOfType));
        }
Exemple #2
0
        public static object TranslateToConvertibleGenericICollectionCache(
            object from, Type toInstanceOfType, Type fromElementType)
        {
            var typeKey = new ConvertibleTypeKey(toInstanceOfType, fromElementType);
            Func <object, Type, object> translateToFn;

            lock (TranslateICollectionCache)
            {
                if (!TranslateConvertibleICollectionCache.TryGetValue(typeKey, out translateToFn))
                {
                    var toElementType = toInstanceOfType.GetGenericType().GetGenericArguments()[0];
                    var genericType   = typeof(TranslateListWithConvertibleElements <,>)
                                        .MakeGenericType(fromElementType, toElementType);

                    var mi = genericType.GetMethod("LateBoundTranslateToGenericICollection",
                                                   BindingFlags.Static | BindingFlags.Public);

                    translateToFn = (Func <object, Type, object>)
                                    Delegate.CreateDelegate(typeof(Func <object, Type, object>), mi);

                    TranslateConvertibleICollectionCache[typeKey] = translateToFn;
                }
            }

            return(translateToFn(from, toInstanceOfType));
        }
 public bool Equals(ConvertibleTypeKey other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.ToInstanceType, ToInstanceType) && Equals(other.FromElemenetType, FromElemenetType));
 }
Exemple #4
0
 public bool Equals(ConvertibleTypeKey other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(other.ToInstanceType == ToInstanceType && other.FromElementType == FromElementType);
 }