/// <summary> /// Tries to find a value from <paramref name="dictionary"/> with types as keys. If direct lookup fails, this method will accept value of the key which will return <c>true</c> for <see cref="E_CommonUtils.IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes"/> with <paramref name="type"/> as first argument and current key as second argument. /// </summary> /// <typeparam name="TValue">The type of the values of the <paramref name="dictionary"/>.</typeparam> /// <param name="type">The type to search value for.</param> /// <param name="dictionary">The dictionary to search from.</param> /// <param name="result">This will contain result if return value is <c>true</c>; otherwise <c>default(TValue)</c>.</param> /// <returns>If the dictionary contains key <paramref name="type"/> or any of its parent types, <c>true</c>; otherwise, <c>false</c>.</returns> internal static Boolean TryFindInTypeDictionarySearchSubTypes <TValue>(this DictionaryQuery <Type, TValue> dictionary, Type type, out TValue result) { // First try to find directly var found = dictionary.TryGetValue(type, out result); if (!found) { // Then iterate all value until found foreach (var kvp in dictionary) { found = type.IsAssignableFrom_IgnoreGenericArgumentsForGenericTypes(kvp.Key); if (found) { result = kvp.Value; break; } } } return(found); }
/// <summary> /// Tries to find a value from <paramref name="dictionary"/> with types as keys. If direct lookup fails, this method will accept value of bottom-most type of <paramref name="type"/>'s inheritance hierarchy found in <paramref name="dictionary"/>. /// </summary> /// <typeparam name="TValue">The type of the values of the <paramref name="dictionary"/>.</typeparam> /// <param name="type">The type to search value for.</param> /// <param name="dictionary">The dictionary to search from.</param> /// <param name="result">This will contain result if return value is <c>true</c>; otherwise <c>default(TValue)</c>.</param> /// <returns>If the dictionary contains key <paramref name="type"/> or any of the keys has <paramref name="type"/> as its parent type, <c>true</c>; otherwise, <c>false</c>.</returns> internal static Boolean TryFindInTypeDictionarySearchBottommostType <TValue>(this DictionaryQuery <Type, TValue> dictionary, Type type, out TValue result) { var found = dictionary.TryGetValue(type, out result); if (!found) { // Search for bottom-most type var current = type; var currentOK = false; foreach (var kvp in dictionary) { currentOK = current.IsAssignableFrom(kvp.Key); found = currentOK || found; if (currentOK) { result = kvp.Value; current = kvp.Key; } } } return(found); }