Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Methuselah96
Copy link

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);
    }
}

@Methuselah96 Methuselah96 changed the title Fix copying custom attributes with array constructor arguments Fix crash when copying custom attributes with array constructor arguments Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failure when using generic structs in nullable contexts
1 participant