// Copyright (c) Microsoft. All rights reserved. using System.Diagnostics.CodeAnalysis; using Microsoft.Extensions.AI; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.AI; /// /// Contains extension methods to allow storing and retrieving properties using the type name of the property as the key. /// public static class AdditionalPropertiesExtensions { /// /// Adds an additional property using the type name of the property as the key. /// /// The type of the property to add. /// The dictionary of additional properties. /// The value to add. public static void Add(this AdditionalPropertiesDictionary additionalProperties, T value) { _ = Throw.IfNull(additionalProperties); additionalProperties.Add(typeof(T).FullName!, value); } /// /// Attempts to add a property using the type name of the property as the key. /// /// /// This method uses the full name of the type parameter as the key. If the key already exists, /// the value is not updated and the method returns . /// /// The type of the property to add. /// The dictionary of additional properties. /// The value to add. /// /// if the value was added successfully; if the key already exists. /// public static bool TryAdd(this AdditionalPropertiesDictionary additionalProperties, T value) { _ = Throw.IfNull(additionalProperties); return additionalProperties.TryAdd(typeof(T).FullName!, value); } /// /// Attempts to retrieve a value from the additional properties dictionary using the type name of the property as the key. /// /// /// This method uses the full name of the type parameter as the key when searching the dictionary. /// /// The type of the property to be retrieved. /// The dictionary containing additional properties. /// /// When this method returns, contains the value retrieved from the dictionary, if found and successfully converted to the requested type; /// otherwise, the default value of . /// /// /// if a non- value was found /// in the dictionary and converted to the requested type; otherwise, . /// public static bool TryGetValue(this AdditionalPropertiesDictionary additionalProperties, [NotNullWhen(true)] out T? value) { _ = Throw.IfNull(additionalProperties); return additionalProperties.TryGetValue(typeof(T).FullName!, out value); } /// /// Determines whether the additional properties dictionary contains a property with the name of the provided type as the key. /// /// The type of the property to check for. /// The dictionary of additional properties. /// /// if the dictionary contains a property with the name of the provided type as the key; otherwise, . /// public static bool Contains(this AdditionalPropertiesDictionary additionalProperties) { _ = Throw.IfNull(additionalProperties); return additionalProperties.ContainsKey(typeof(T).FullName!); } /// /// Removes a property from the additional properties dictionary using the name of the provided type as the key. /// /// The type of the property to remove. /// The dictionary of additional properties. /// /// if the property was successfully removed; otherwise, . /// public static bool Remove(this AdditionalPropertiesDictionary additionalProperties) { _ = Throw.IfNull(additionalProperties); return additionalProperties.Remove(typeof(T).FullName!); } }