BO4E/UserPropertiesExtensions.cs
/// <summary>
///     try to get the Userproperty with key <paramref name="userPropertyKey" /> from <paramref name="parent" /> if
///     <paramref name="parent" />.UserProperties is not null and the key is present.
/// </summary>
/// <typeparam name="TUserProperty">
///     type expected to be found in the User Property with key
///     <paramref name="userPropertyKey" />
/// </typeparam>
/// <typeparam name="TParent">class implementing <see cref="IUserProperties" /></typeparam>
/// <param name="parent">object implementing <see cref="IUserProperties" /></param>
/// <param name="userPropertyKey">
///     key under which the <paramref name="value" /> is expected in <paramref name="parent" />
///     .UserProperties
/// </param>
/// <param name="value">default if false is returned, the stored value otherwise</param>
/// <returns>true if found</returns>
/// <exception cref="ArgumentNullException">iff <paramref name="userPropertyKey" /> is null or whitespace</exception>
try to get the Userproperty with key  from  if
.UserProperties is not null and the key is present.
try to get the Userproperty with key from if . UserProperties is not null and the key is present .
public static bool TryGetUserProperty<TUserProperty, TParent>(this TParent parent, string userPropertyKey,
            out TUserProperty value) where TParent : IUserProperties
        {
            var up = parent.UserProperties;
            if (string.IsNullOrWhiteSpace(userPropertyKey)) throw new ArgumentNullException(nameof(userPropertyKey));
            if (up != null && up.TryGetValue(userPropertyKey, out var upToken))
            {
                switch (upToken)
                {
                    case null:
                        value = default;
                        return false;
                    case string token:
                        value = new JValue(token).Value<TUserProperty>();
                        break;
                    case double d:
                        value = new JValue(d).Value<TUserProperty>();
                        break;
                    case long l:
                        value = new JValue(l).Value<TUserProperty>();
                        break;
                    case bool b:
                        value = new JValue(b).Value<TUserProperty>();
                        break;
                    case JValue jValue:
                        value = jValue.Value<TUserProperty>();
                        break;
                    default:
                        try
                        {
                            value = JsonSerializer.Deserialize<TUserProperty>(((JsonElement)upToken).GetRawText(),
                                Defaults.JsonSerializerDefaultOptions);
                        }
                        catch (JsonException)
                        {
                            throw new FormatException(
                                $"Could not convert {((JsonElement)upToken).GetRawText()} to {typeof(TUserProperty).Name}");
                        }
                        break;
                }
                return true;
            }
            value = default;
            return false;
        }
public static bool TryGetUserProperty < TUserProperty , TParent > ( this TParent parent , string userPropertyKey , out TUserProperty value ) where TParent : IUserProperties { var up = parent . UserProperties ; if ( string . IsNullOrWhiteSpace ( userPropertyKey ) ) throw new ArgumentNullException ( nameof ( userPropertyKey ) ) ; if ( up != null && up . TryGetValue ( userPropertyKey , out var upToken ) ) { switch ( upToken ) { case null : value = default ; return false ; case string token : value = new JValue ( token ) . Value < TUserProperty > ( ) ; break ; case double d : value = new JValue ( d ) . Value < TUserProperty > ( ) ; break ; case long l : value = new JValue ( l ) . Value < TUserProperty > ( ) ; break ; case bool b : value = new JValue ( b ) . Value < TUserProperty > ( ) ; break ; case JValue jValue : value = jValue . Value < TUserProperty > ( ) ; break ; default : try { value = JsonSerializer . Deserialize < TUserProperty > ( ( ( JsonElement ) upToken ) . GetRawText ( ) , Defaults . JsonSerializerDefaultOptions ) ; } catch ( JsonException ) { throw new FormatException ( $" Could not convert  { ( ( JsonElement ) upToken ) . GetRawText ( ) }  to  { typeof ( TUserProperty ) . Name } " ) ; } break ; } return true ; } value = default ; return false ; }
try to get the Userproperty with key  from  if
.UserProperties is not null and the key is present.
try to get the Userproperty with key from if . UserProperties is not null and the key is present .
true if found
true if found
iff  is null or whitespace
iff is null or whitespace
TParent
this
userPropertyKey
string
key under which the  is expected in
.UserProperties
key under which the is expected in . UserProperties
TUserProperty
out
parent
None
value
default if false is returned, the stored value otherwise
default if false is returned the stored value otherwise
typeparam
type expected to be found in the User Property with key
type expected to be found in the User Property with key
typeparam
None