/// <summary>Translate to convertible generic i collection cache.</summary> /// <param name="from"> Source for the.</param> /// <param name="toInstanceOfType">Type of to instance of.</param> /// <param name="fromElementType"> Type of from element.</param> /// <returns>An object.</returns> 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)); }
/// <summary> /// Determines whether the specified <see cref="T:System.Object" /> is equal to the current /// <see cref="T:System.Object" />. /// </summary> /// <param name="other">The convertible type key to compare to this object.</param> /// <returns> /// true if the specified <see cref="T:System.Object" /> is equal to the current /// <see cref="T:System.Object" />; otherwise, false. /// </returns> 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)); }