123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- namespace Ips.Library.Basic
- {
- public static class DictionaryExtensions
- {
- internal static bool TryGetValue<T>(this IDictionary<string, object> dictionary, string key, out T value)
- {
- object valueObj;
- if (dictionary.TryGetValue(key, out valueObj) && valueObj is T)
- {
- value = (T)valueObj;
- return true;
- }
- value = default;
- return false;
- }
- public static TValue GetOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
- {
- TValue obj;
- return dictionary.TryGetValue(key, out obj) ? obj : default;
- }
- public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
- {
- return dictionary.TryGetValue(key, out var obj) ? obj : default;
- }
- public static TValue GetOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
- {
- return dictionary.TryGetValue(key, out var obj) ? obj : default;
- }
- public static TValue GetOrDefault<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key)
- {
- return dictionary.TryGetValue(key, out var obj) ? obj : default;
- }
- public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> factory)
- {
- TValue obj;
- if (dictionary.TryGetValue(key, out obj))
- {
- return obj;
- }
- return dictionary[key] = factory(key);
- }
- public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> factory)
- {
- return dictionary.GetOrAdd(key, k => factory());
- }
- public static TValue GetOrAdd<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> factory)
- {
- return dictionary.GetOrAdd(key, k => factory());
- }
- }
- }
|