public static Attribute ToReflection()

in src/Microsoft.NET.Sdk.Functions.Generator/TypeUtility.cs [104:131]


        public static Attribute ToReflection(this CustomAttribute customAttribute)
        {
            var attributeType = customAttribute.AttributeType.ToReflectionType();
            if (attributeType == null)
            {
                throw new InvalidOperationException($"Could not load type '{customAttribute.AttributeType}'.");
            }

            Type[] constructorParams = customAttribute.Constructor.Parameters
                 .Select(p => p.ParameterType.ToReflectionType())
                 .ToArray();

            ConstructorInfo ctor = attributeType.GetConstructor(constructorParams);
            if (ctor == null)
            {
                throw new InvalidOperationException($"Could not find constructor for type {attributeType} matching params [{string.Join(",", constructorParams.Select(t => t.ToString()))}].");
            }

            Attribute attribute = ctor.Invoke(customAttribute.ConstructorArguments.Select(p => NormalizeArg(p)).ToArray()) as Attribute;

            foreach (var namedArgument in customAttribute.Properties)
            {
                attributeType.GetProperty(namedArgument.Name)?.SetValue(attribute, namedArgument.Argument.Value);
                attributeType.GetField(namedArgument.Name)?.SetValue(attribute, namedArgument.Argument.Value);
            }

            return attribute;
        }