/// <summary> /// Converts an attribute /// </summary> /// <param name="a">Builder to convert</param> /// <param name="isThisMemberType">Whether the conversion is to be applied /// to the member type it was defined on</param> /// <param name="currentTarget">The member type to be applied on</param> /// <param name="builder">Output builder</param> /// <returns>True if converted, false if could not convert</returns> internal bool Convert(CustomAttributeBuilderBuilder a, bool isThisMemberType, AttributeTargets currentTarget, out CustomAttributeBuilderBuilder builder) { var att = a.BuildAttribute(); var converter = GetConverter(att.GetType(), isThisMemberType); var convertFunc = converter?._conversion; if (convertFunc != null) { builder = convertFunc(a); if (builder != null && !currentTarget.ValidOn(builder.AttributeType, out _)) { builder = null; } } else { builder = null; } return(convertFunc != null); }
/// <summary> /// Common logic for handling the data on an IAttributeConverter and applying it to a member /// </summary> /// <param name="converter">The IAttributeConverter whose data is being applied</param> /// <param name="targets">The target to which the attribute is being applied</param> /// <param name="adder">A function to add an attribute to the member. Necessary because /// SetCustomAttribute is not on a common interface on the member Builder types.</param> /// <param name="customAttributeData">The data on the attributes to convert</param> internal static void HandleAttributes(this IAttributeConverter converter, AttributeTargets targets, Action <CustomAttributeBuilder> adder, CustomAttributeData[] customAttributeData) { foreach (var attData in customAttributeData) { var cBuilder = new CustomAttributeBuilderBuilder(attData); var att = cBuilder.BuildAttribute(); if (converter.Convert(cBuilder, true, targets, out CustomAttributeBuilderBuilder aBuilder)) { if (aBuilder != null) { adder(aBuilder.Build()); } } else { adder(cBuilder.Build()); } } foreach (var newAttBuilder in converter.GetNewAttributes()) { adder(newAttBuilder.Build()); } }