// // AttributeInfoExtensions.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Diagnostics; using NosSmooth.PacketSerializersGenerator.Data; namespace NosSmooth.PacketSerializersGenerator.Extensions; /// /// Extension methods for . /// public static class AttributeInfoExtensions { /// /// Get value of a named parameter. /// /// The attribute information. /// The name of the parameter. /// The default value to return if not found. /// The value type. /// The value of the attribute. public static TValue? GetNamedValue(this AttributeInfo attributeInfo, string name, TValue? @default) { if (attributeInfo.NamedAttributeArguments.TryGetValue(name, out var value)) { if (typeof(TValue) == typeof(string)) { return (TValue?)(object?)value?.ToString(); } return (TValue?)value; } return @default; } /// /// Get value of a named parameter. /// /// The attribute information. /// The index of the parameter. /// The value type. /// The value of the attribute. public static TValue? GetIndexedValue(this AttributeInfo attributeInfo, int index) { var value = attributeInfo.IndexedAttributeArguments[index]; if (typeof(TValue) == typeof(string)) { return (TValue?)(object?)value?.ToString(); } return (TValue?)value; } }