Skip to content

Commit

Permalink
chore:merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
MayueCif committed Jun 28, 2023
2 parents af42902 + 80033f7 commit ad313ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ 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)
if (dictionary.TryGetValue(key, out valueObj!) && valueObj is T)
{
value = (T)valueObj;
return true;
}

value = default;
value = default!;
return false;
}

Expand Down Expand Up @@ -77,9 +77,15 @@ internal static bool TryGetValue<T>(this IDictionary<string, object> dictionary,
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TKey, TValue> factory)
public static TValue? GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TKey, TValue> factory)
{
TValue obj;
TValue? obj;

if (key == null)
{
throw new KeyNotFoundException();
}

if (dictionary.TryGetValue(key, out obj))
{
return obj;
Expand All @@ -97,7 +103,7 @@ public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dicti
/// <typeparam name="TKey">Type of the key</typeparam>
/// <typeparam name="TValue">Type of the value</typeparam>
/// <returns>Value if found, default if can not found.</returns>
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TValue> factory)
public static TValue? GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, [NotNull] TKey key, Func<TValue> factory)
{
return dictionary.GetOrAdd(key, k => factory());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public static bool HasProperty(this ExtraPropertyDictionary source, string name)
return source.ContainsKey(name);
}

public static object GetProperty(this ExtraPropertyDictionary source, string name, object defaultValue = null)
public static object? GetProperty(this ExtraPropertyDictionary source, string name, object? defaultValue = null)
{
return source.GetOrDefault(name)
?? defaultValue;
}

public static TProperty GetProperty<TProperty>(this ExtraPropertyDictionary source, string name, TProperty defaultValue = default)
public static TProperty? GetProperty<TProperty>(this ExtraPropertyDictionary source, string name, TProperty? defaultValue = default)
{
var value = source.GetProperty(name);
if (value == null)
Expand All @@ -34,15 +34,15 @@ public static TProperty GetProperty<TProperty>(this ExtraPropertyDictionary sour

if (conversionType == typeof(Guid))
{
return (TProperty)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString());
return (TProperty?)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString()!);
}

if (conversionType == typeof(DateTimeOffset))
{
return (TProperty)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString());
return (TProperty?)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString()!);
}

return (TProperty)Convert.ChangeType(value?.ToString(), conversionType, CultureInfo.InvariantCulture);
return (TProperty?)Convert.ChangeType(value?.ToString(), conversionType, CultureInfo.InvariantCulture);
}

throw new Exception("GetProperty<TProperty> does not support non-primitive types. Use non-generic GetProperty method and handle type casting manually.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static Type GetFirstGenericArgumentIfNullable(this Type t)
{
if (t.GetGenericArguments().Length > 0 && t.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return t.GetGenericArguments().FirstOrDefault();
return t.GetGenericArguments().FirstOrDefault() ?? t;
}

return t;
Expand Down Expand Up @@ -148,7 +148,7 @@ public static object ConvertFrom(Type targetType, object value)
{
return TypeDescriptor
.GetConverter(targetType)
.ConvertFrom(value);
.ConvertFrom(value)?? value;
}

public static Type StripNullable(Type type)
Expand Down

0 comments on commit ad313ad

Please sign in to comment.