using System.Collections.Generic; using Microsoft.CodeAnalysis; namespace ComputeSharp.SourceGeneration.Extensions; /// /// Extension methods for the type. /// internal static class AttributeDataExtensions { /// /// Tries to get a constructor argument at a given index from the input instance. /// /// The type of constructor argument to retrieve. /// The target instance to get the argument from. /// The index of the argument to try to retrieve. /// The resulting argument, if it was found. /// Whether or not an argument of type at position was found. public static bool TryGetConstructorArgument(this AttributeData attributeData, int index, out T? result) { if (attributeData.ConstructorArguments.Length >= index + 1 && attributeData.ConstructorArguments[index].Value is T argument) { result = argument; return true; } result = default; return false; } /// /// Tries to get a given named argument value from an instance, if present. /// /// The type of argument to check. /// The target instance to check. /// The name of the argument to check. /// The resulting argument value, if present. /// Whether or not contains an argument named with a valid value. public static bool TryGetNamedArgument(this AttributeData attributeData, string name, out T? value) { foreach (KeyValuePair properties in attributeData.NamedArguments) { if (properties.Key == name) { value = (T?)properties.Value.Value; return true; } } value = default; return false; } }