public static object TranslateToGenericICollectionCache(object from, Type toInstanceOfType, Type elementType) { ConvertInstanceDelegate instanceDelegate1; if (TranslateListWithElements.TranslateICollectionCache.TryGetValue(toInstanceOfType, out instanceDelegate1)) { return(instanceDelegate1(from, toInstanceOfType)); } ConvertInstanceDelegate instanceDelegate2 = (ConvertInstanceDelegate)typeof(TranslateListWithElements <>).MakeGenericType(elementType).GetStaticMethod("LateBoundTranslateToGenericICollection").MakeDelegate(typeof(ConvertInstanceDelegate), true); Dictionary <Type, ConvertInstanceDelegate> icollectionCache; Dictionary <Type, ConvertInstanceDelegate> dictionary; do { icollectionCache = TranslateListWithElements.TranslateICollectionCache; dictionary = new Dictionary <Type, ConvertInstanceDelegate>((IDictionary <Type, ConvertInstanceDelegate>)TranslateListWithElements.TranslateICollectionCache); dictionary[elementType] = instanceDelegate2; }while (Interlocked.CompareExchange <Dictionary <Type, ConvertInstanceDelegate> >(ref TranslateListWithElements.TranslateICollectionCache, dictionary, icollectionCache) != icollectionCache); return(instanceDelegate2(from, toInstanceOfType)); }
public static object TranslateToConvertibleGenericICollectionCache(object from, Type toInstanceOfType, Type fromElementType) { ConvertibleTypeKey key = new ConvertibleTypeKey(toInstanceOfType, fromElementType); ConvertInstanceDelegate instanceDelegate1; if (TranslateListWithElements.TranslateConvertibleICollectionCache.TryGetValue(key, out instanceDelegate1)) { return(instanceDelegate1(from, toInstanceOfType)); } Type genericTypeArgument = toInstanceOfType.FirstGenericType().GenericTypeArguments()[0]; ConvertInstanceDelegate instanceDelegate2 = (ConvertInstanceDelegate)typeof(TranslateListWithConvertibleElements <,>).MakeGenericType(fromElementType, genericTypeArgument).GetStaticMethod("LateBoundTranslateToGenericICollection").MakeDelegate(typeof(ConvertInstanceDelegate), true); Dictionary <ConvertibleTypeKey, ConvertInstanceDelegate> icollectionCache; Dictionary <ConvertibleTypeKey, ConvertInstanceDelegate> dictionary; do { icollectionCache = TranslateListWithElements.TranslateConvertibleICollectionCache; dictionary = new Dictionary <ConvertibleTypeKey, ConvertInstanceDelegate>((IDictionary <ConvertibleTypeKey, ConvertInstanceDelegate>)TranslateListWithElements.TranslateConvertibleICollectionCache); dictionary[key] = instanceDelegate2; }while (Interlocked.CompareExchange <Dictionary <ConvertibleTypeKey, ConvertInstanceDelegate> >(ref TranslateListWithElements.TranslateConvertibleICollectionCache, dictionary, icollectionCache) != icollectionCache); return(instanceDelegate2(from, toInstanceOfType)); }