Пример #1
0
        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));
        }
Пример #2
0
 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));
 }