Files
mixtape/zero.Core/Extensions/DictionaryExtensions.cs
T
2021-03-02 16:16:29 +01:00

27 lines
651 B
C#

using System.Collections.Generic;
namespace zero.Core.Extensions
{
public static class DictionaryExtensions
{
public static bool TryGetValue<T>(this Dictionary<string, object> model, string key, out T value)
{
if (!model.TryGetValue(key, out object valueObj) || !(valueObj is T))
{
value = default;
return false;
}
value = (T)valueObj;
return true;
}
public static T GetValueOrDefault<T>(this Dictionary<string, object> model, string key)
{
object value = model.GetValueOrDefault(key);
return value == default || !(value is T) ? default : (T)value;
}
}
}