ekonbenefits / impromptu-interface

Static interface to dynamic implementation (duck casting). Uses the DLR combined with Reflect.Emit.
Apache License 2.0
657 stars 67 forks source link

Fix crash when copying custom attributes with array constructor arguments #57

Open Methuselah96 opened 11 months ago

Methuselah96 commented 11 months ago

Fixes #56.

As noted by the CustomAttributeTypedArgument docs:

If an argument is an array of values, the Value property of the CustomAttributeTypedArgument that represents the argument returns a generic ReadOnlyCollection of CustomAttributeTypedArgument objects. Each CustomAttributeTypedArgument object in the collection represents the corresponding element of the array.

Therefore, the code that translates the CustomAttributeData to CustomAttributeBuilder needs to convert ReadOnlyCollection constructor arguments to arrays. Example code showing the legitimacy of checking for ReadOnlyCollections is shown in the docs examples:

private static void ShowValueOrArray(CustomAttributeTypedArgument cata)
{
    if (cata.Value.GetType() == typeof(ReadOnlyCollection<CustomAttributeTypedArgument>))
    {
        Console.WriteLine("         Array of '{0}':", cata.ArgumentType);

        foreach (CustomAttributeTypedArgument cataElement in
            (ReadOnlyCollection<CustomAttributeTypedArgument>) cata.Value)
        {
            Console.WriteLine("             Type: '{0}'  Value: '{1}'",
                cataElement.ArgumentType, cataElement.Value);
        }
    }
    else
    {
        Console.WriteLine("         Type: '{0}'  Value: '{1}'",
            cata.ArgumentType, cata.Value);
    }
}