From 88be17003e15797fddaece17eca7a7ac4b9c8d76 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Wed, 11 Dec 2019 12:34:36 +0100 Subject: [PATCH] Output: Don't include nested generic type parameters in generic type usages (CS0305) --- Il2CppInspector/Reflection/TypeInfo.cs | 28 +++++++++++-------- .../GameAssembly-GenericTypes-x64.cs | 8 +++--- .../GameAssembly-GenericTypes-x86.cs | 8 +++--- .../TestExpectedResults/GenericTypes.cs | 8 +++--- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Il2CppInspector/Reflection/TypeInfo.cs b/Il2CppInspector/Reflection/TypeInfo.cs index 1b9beee..4c9f196 100644 --- a/Il2CppInspector/Reflection/TypeInfo.cs +++ b/Il2CppInspector/Reflection/TypeInfo.cs @@ -66,18 +66,22 @@ namespace Il2CppInspector.Reflection { } // C# name as it would be written in a type declaration - public string CSharpTypeDeclarationName => - (IsByRef? "ref " : "") - + (HasElementType? - ElementType.CSharpTypeDeclarationName : - ((GenericParameterAttributes & GenericParameterAttributes.Contravariant) == GenericParameterAttributes.Contravariant? "in " : "") - + ((GenericParameterAttributes & GenericParameterAttributes.Covariant) == GenericParameterAttributes.Covariant? "out ":"") - + (base.Name.IndexOf("`", StringComparison.Ordinal) == -1 ? base.Name : base.Name.Remove(base.Name.IndexOf("`", StringComparison.Ordinal))) - + ((IsNested? GenericTypeParameters?.Where(p => DeclaringType.GenericTypeParameters?.All(dp => dp.Name != p.Name) ?? true) : GenericTypeParameters)?.Any() ?? false? - "<" + string.Join(", ", GenericTypeParameters.Select(x => x.CSharpTypeDeclarationName)) + ">" : "") - + (GenericTypeArguments != null ? "<" + string.Join(", ", GenericTypeArguments.Select(x => (!x.IsGenericTypeParameter? x.Namespace + "." : "") + x.CSharpTypeDeclarationName)) + ">" : "")) - + (IsArray ? "[" + new string(',', GetArrayRank() - 1) + "]" : "") - + (IsPointer ? "*" : ""); + public string CSharpTypeDeclarationName { + get { + var gtp = IsNested? GenericTypeParameters?.Where(p => DeclaringType.GenericTypeParameters?.All(dp => dp.Name != p.Name) ?? true) : GenericTypeParameters; + + return (IsByRef ? "ref " : "") + + (HasElementType + ? ElementType.CSharpTypeDeclarationName + : ((GenericParameterAttributes & GenericParameterAttributes.Contravariant) == GenericParameterAttributes.Contravariant ? "in " : "") + + ((GenericParameterAttributes & GenericParameterAttributes.Covariant) == GenericParameterAttributes.Covariant ? "out " : "") + + (base.Name.IndexOf("`", StringComparison.Ordinal) == -1 ? base.Name : base.Name.Remove(base.Name.IndexOf("`", StringComparison.Ordinal))) + + (gtp?.Any() ?? false? "<" + string.Join(", ", gtp.Select(x => x.CSharpTypeDeclarationName)) + ">" : "") + + (GenericTypeArguments != null ? "<" + string.Join(", ", GenericTypeArguments.Select(x => (!x.IsGenericTypeParameter ? x.Namespace + "." : "") + x.CSharpTypeDeclarationName)) + ">" : "")) + + (IsArray ? "[" + new string(',', GetArrayRank() - 1) + "]" : "") + + (IsPointer ? "*" : ""); + } + } // Custom attributes for this member public override IEnumerable CustomAttributes => CustomAttributeData.GetCustomAttributes(this); diff --git a/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x64.cs b/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x64.cs index 74b1738..51ef212 100644 --- a/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x64.cs +++ b/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x64.cs @@ -138,20 +138,20 @@ namespace Il2CppTests.TestSources public NestedWithAutomaticConstraints() {} } - private class NestedWithNewGenericParameter // TypeDefIndex: 1823 + private class NestedWithNewGenericParameter // TypeDefIndex: 1823 { // Constructors public NestedWithNewGenericParameter() {} } - private class NestedWithNewGenericParameterAndConstraint // TypeDefIndex: 1824 + private class NestedWithNewGenericParameterAndConstraint // TypeDefIndex: 1824 where T : new() { // Constructors public NestedWithNewGenericParameterAndConstraint() {} } - private class NestedWithNewGenericParameterAndDependentConstraint // TypeDefIndex: 1825 + private class NestedWithNewGenericParameterAndDependentConstraint // TypeDefIndex: 1825 where T : G, new() { // Constructors @@ -211,7 +211,7 @@ namespace Il2CppTests.TestSources public SubInnerGeneric2() {} } - public class SubInnerGeneric3 // TypeDefIndex: 1832 + public class SubInnerGeneric3 // TypeDefIndex: 1832 { // Constructors public SubInnerGeneric3() {} diff --git a/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x86.cs b/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x86.cs index 51bf020..66f0bad 100644 --- a/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x86.cs +++ b/Il2CppTests/TestExpectedResults/GameAssembly-GenericTypes-x86.cs @@ -138,20 +138,20 @@ namespace Il2CppTests.TestSources public NestedWithAutomaticConstraints() {} } - private class NestedWithNewGenericParameter // TypeDefIndex: 1823 + private class NestedWithNewGenericParameter // TypeDefIndex: 1823 { // Constructors public NestedWithNewGenericParameter() {} } - private class NestedWithNewGenericParameterAndConstraint // TypeDefIndex: 1824 + private class NestedWithNewGenericParameterAndConstraint // TypeDefIndex: 1824 where T : new() { // Constructors public NestedWithNewGenericParameterAndConstraint() {} } - private class NestedWithNewGenericParameterAndDependentConstraint // TypeDefIndex: 1825 + private class NestedWithNewGenericParameterAndDependentConstraint // TypeDefIndex: 1825 where T : G, new() { // Constructors @@ -211,7 +211,7 @@ namespace Il2CppTests.TestSources public SubInnerGeneric2() {} } - public class SubInnerGeneric3 // TypeDefIndex: 1832 + public class SubInnerGeneric3 // TypeDefIndex: 1832 { // Constructors public SubInnerGeneric3() {} diff --git a/Il2CppTests/TestExpectedResults/GenericTypes.cs b/Il2CppTests/TestExpectedResults/GenericTypes.cs index c48e8e4..26aa918 100644 --- a/Il2CppTests/TestExpectedResults/GenericTypes.cs +++ b/Il2CppTests/TestExpectedResults/GenericTypes.cs @@ -138,20 +138,20 @@ namespace Il2CppTests.TestSources public NestedWithAutomaticConstraints() {} } - private class NestedWithNewGenericParameter // TypeDefIndex: 1823 + private class NestedWithNewGenericParameter // TypeDefIndex: 1823 { // Constructors public NestedWithNewGenericParameter() {} } - private class NestedWithNewGenericParameterAndConstraint // TypeDefIndex: 1824 + private class NestedWithNewGenericParameterAndConstraint // TypeDefIndex: 1824 where T : new() { // Constructors public NestedWithNewGenericParameterAndConstraint() {} } - private class NestedWithNewGenericParameterAndDependentConstraint // TypeDefIndex: 1825 + private class NestedWithNewGenericParameterAndDependentConstraint // TypeDefIndex: 1825 where T : G, new() { // Constructors @@ -211,7 +211,7 @@ namespace Il2CppTests.TestSources public SubInnerGeneric2() {} } - public class SubInnerGeneric3 // TypeDefIndex: 1832 + public class SubInnerGeneric3 // TypeDefIndex: 1832 { // Constructors public SubInnerGeneric3() {}