Skip to content

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

Merged
jbtule merged 3 commits into
ekonbenefits:masterfrom
Methuselah96:fix-attribute-copying
Nov 16, 2024
Merged

jbtule merged 3 commits into
ekonbenefits:masterfrom
Methuselah96:fix-attribute-copying

Conversation

@Methuselah96

Copy link
Copy Markdown
Contributor

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
@jbtule
jbtule merged commit 5e70e34 into ekonbenefits:master Nov 16, 2024
@Methuselah96
Methuselah96 deleted the fix-attribute-copying branch November 16, 2024 13:34
@Methuselah96

Copy link
Copy Markdown
Contributor Author

@jbtule Any idea when a new version might be published to NuGet with this fix?

@jbtule

jbtule commented Jan 8, 2025

Copy link
Copy Markdown
Member

@Methuselah96 I uploaded it to nuget just now. it's currently validating and indexing.

@Methuselah96

Copy link
Copy Markdown
Contributor Author

Thanks!

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

2 participants