Skip to content

Commit ba67c11

Browse files
authored
Switch from using AttributeAttribute::Name to AttributeAttribute::Type (#194)
* Switch from using `AttributeAttribute::Name` to `AttributeAttribute::Type` * Support backwards compatibility
1 parent f2b2e69 commit ba67c11

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Il2CppInterop.Generator/Passes/Pass70GenerateProperties.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,13 @@ public static void DoPass(RewriteGlobalContext context)
4242
}
4343

4444
string? defaultMemberName = null;
45-
var defaultMemberAttributeAttribute = type.CustomAttributes.FirstOrDefault(it =>
46-
it.AttributeType()?.Name == "AttributeAttribute" && it.Signature!.NamedArguments.Any(it =>
47-
it.MemberName == "Name" && it.Argument.GetElementAsString() == nameof(DefaultMemberAttribute)));
48-
if (defaultMemberAttributeAttribute != null)
45+
if (type.CustomAttributes.FirstOrDefault(IsDefaultMemberAttributeFake) != null)
4946
{
5047
defaultMemberName = "Item";
5148
}
5249
else
5350
{
54-
var realDefaultMemberAttribute =
55-
type.CustomAttributes.FirstOrDefault(it => it.AttributeType()?.Name == nameof(DefaultMemberAttribute));
51+
var realDefaultMemberAttribute = type.CustomAttributes.FirstOrDefault(IsDefaultMemberAttributeReal);
5652
if (realDefaultMemberAttribute != null)
5753
defaultMemberName = realDefaultMemberAttribute.Signature?.FixedArguments[0].Element?.ToString() ?? "Item";
5854
}
@@ -63,6 +59,21 @@ public static void DoPass(RewriteGlobalContext context)
6359
assemblyContext.Imports.Module.DefaultMemberAttribute().ToTypeDefOrRef(), assemblyContext.Imports.Module.String()),
6460
new CustomAttributeSignature(new CustomAttributeArgument(assemblyContext.Imports.Module.String(), defaultMemberName))));
6561
}
62+
63+
static bool IsDefaultMemberAttributeFake(CustomAttribute attribute)
64+
{
65+
return attribute.AttributeType()?.Name == "AttributeAttribute" && attribute.Signature!.NamedArguments.Any(it =>
66+
{
67+
// Name support is for backwards compatibility.
68+
return (it.MemberName == "Type" && it.Argument.Element is ITypeDescriptor { Namespace: "System.Reflection", Name: nameof(DefaultMemberAttribute) })
69+
|| (it.MemberName == "Name" && it.Argument.GetElementAsString() == nameof(DefaultMemberAttribute));
70+
});
71+
}
72+
73+
static bool IsDefaultMemberAttributeReal(CustomAttribute attribute)
74+
{
75+
return attribute.AttributeType() is { Namespace.Value: "System.Reflection", Name.Value: nameof(DefaultMemberAttribute) };
76+
}
6677
}
6778

6879
private static string UnmanglePropertyName(AssemblyRewriteContext assemblyContext, PropertyDefinition prop,

0 commit comments

Comments
 (0)