/// <summary>
        /// Tries to get the value for the specified key.
        /// </summary>
        /// <remarks>
        /// This method will also materialize <see cref="IMetadataValue"/> and
        /// evaluate script strings (a key that starts with "=>" will be treated
        /// as a script and evaluated).
        /// </remarks>
        /// <param name="metadata">The metadata instance.</param>
        /// <typeparam name="TValue">The desired return type.</typeparam>
        /// <param name="key">The key of the value to get. If the key is <c>null</c>, this will return the default value.</param>
        /// <param name="value">The value of the key if it was found and could be converted to the desired return type.</param>
        /// <returns><c>true</c> if the key was found and the value could be converted to the desired return type, <c>false</c> otherwise.</returns>
        public static bool TryGetValue <TValue>(
            this IMetadata metadata,
            string key,
            out TValue value)
        {
            if (metadata != null && key != null)
            {
                // Script
                if (IScriptHelper.TryGetScriptString(key, out string script))
                {
                    IExecutionContext context = IExecutionContext.Current;
                    object            result  = context.ScriptHelper.EvaluateAsync(script, metadata).GetAwaiter().GetResult();
                    return(TypeHelper.TryExpandAndConvert(result, metadata, out value));
                }

                // Value
                if (metadata.TryGetRaw(key, out object raw))
                {
                    return(TypeHelper.TryExpandAndConvert(raw, metadata, out value));
                }
            }

            value = default;
            return(false);
        }
Beispiel #2
0
 public static bool TryGetScriptMetadataValue(string key, object value, IExecutionState executionState, out ScriptMetadataValue scriptMetadataValue)
 {
     scriptMetadataValue = default;
     if (value is string stringValue && IScriptHelper.TryGetScriptString(stringValue, out string script))
     {
         scriptMetadataValue = new ScriptMetadataValue(
             key,
             stringValue.Substring(0, stringValue.Length - script.Length),
             script,
             executionState);
         return(true);
     }
     return(false);
 }