Skip to content

Commit

Permalink
Switch from using AttributeAttribute::Name to `AttributeAttribute::…
Browse files Browse the repository at this point in the history
…Type` (#194)

* Switch from using `AttributeAttribute::Name` to `AttributeAttribute::Type`

* Support backwards compatibility
  • Loading branch information
ds5678 authored Dec 23, 2024
1 parent f2b2e69 commit ba67c11
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,13 @@ public static void DoPass(RewriteGlobalContext context)
}

string? defaultMemberName = null;
var defaultMemberAttributeAttribute = type.CustomAttributes.FirstOrDefault(it =>
it.AttributeType()?.Name == "AttributeAttribute" && it.Signature!.NamedArguments.Any(it =>
it.MemberName == "Name" && it.Argument.GetElementAsString() == nameof(DefaultMemberAttribute)));
if (defaultMemberAttributeAttribute != null)
if (type.CustomAttributes.FirstOrDefault(IsDefaultMemberAttributeFake) != null)
{
defaultMemberName = "Item";
}
else
{
var realDefaultMemberAttribute =
type.CustomAttributes.FirstOrDefault(it => it.AttributeType()?.Name == nameof(DefaultMemberAttribute));
var realDefaultMemberAttribute = type.CustomAttributes.FirstOrDefault(IsDefaultMemberAttributeReal);
if (realDefaultMemberAttribute != null)
defaultMemberName = realDefaultMemberAttribute.Signature?.FixedArguments[0].Element?.ToString() ?? "Item";
}
Expand All @@ -63,6 +59,21 @@ public static void DoPass(RewriteGlobalContext context)
assemblyContext.Imports.Module.DefaultMemberAttribute().ToTypeDefOrRef(), assemblyContext.Imports.Module.String()),
new CustomAttributeSignature(new CustomAttributeArgument(assemblyContext.Imports.Module.String(), defaultMemberName))));
}

static bool IsDefaultMemberAttributeFake(CustomAttribute attribute)
{
return attribute.AttributeType()?.Name == "AttributeAttribute" && attribute.Signature!.NamedArguments.Any(it =>
{
// Name support is for backwards compatibility.
return (it.MemberName == "Type" && it.Argument.Element is ITypeDescriptor { Namespace: "System.Reflection", Name: nameof(DefaultMemberAttribute) })
|| (it.MemberName == "Name" && it.Argument.GetElementAsString() == nameof(DefaultMemberAttribute));
});
}

static bool IsDefaultMemberAttributeReal(CustomAttribute attribute)
{
return attribute.AttributeType() is { Namespace.Value: "System.Reflection", Name.Value: nameof(DefaultMemberAttribute) };
}
}

private static string UnmanglePropertyName(AssemblyRewriteContext assemblyContext, PropertyDefinition prop,
Expand Down

0 comments on commit ba67c11

Please sign in to comment.