From 6046f2493d4806e516a39a26c1e1466636717cb4 Mon Sep 17 00:00:00 2001 From: Katy Coe Date: Mon, 24 Feb 2020 12:05:33 +0100 Subject: [PATCH] C#: Reduce scope redundancies in type declarations --- Il2CppInspector.Common/Outputs/CSharpCodeStubs.cs | 10 +++------- Il2CppInspector.Common/Reflection/TypeInfo.cs | 10 ++++++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Il2CppInspector.Common/Outputs/CSharpCodeStubs.cs b/Il2CppInspector.Common/Outputs/CSharpCodeStubs.cs index 552de15..76cadc3 100644 --- a/Il2CppInspector.Common/Outputs/CSharpCodeStubs.cs +++ b/Il2CppInspector.Common/Outputs/CSharpCodeStubs.cs @@ -557,15 +557,11 @@ namespace Il2CppInspector.Outputs sb.Append(prefix + type.GetModifierString()); - // Inheriting from a base class or implementing an interface using a generic type which contains a nested type parameter - // inside the declared class must be referenced from outside the scope of the type being defined - var outerScope = new Scope {Current = scope.Current.DeclaringType, Namespaces = scope.Namespaces}; - - var @base = type.ImplementedInterfaces.Select(x => x.GetScopedCSharpName(outerScope)).ToList(); + var @base = type.ImplementedInterfaces.Select(x => x.GetScopedCSharpName(scope, isPartOfTypeDeclaration: true)).ToList(); if (type.BaseType != null && type.BaseType.FullName != "System.Object" && type.BaseType.FullName != "System.ValueType" && !type.IsEnum) - @base.Insert(0, type.BaseType.GetScopedCSharpName(outerScope)); + @base.Insert(0, type.BaseType.GetScopedCSharpName(scope, isPartOfTypeDeclaration: true)); if (type.IsEnum && type.GetEnumUnderlyingType().FullName != "System.Int32") // enums derive from int by default - @base.Insert(0, type.GetEnumUnderlyingType().GetScopedCSharpName(outerScope)); + @base.Insert(0, type.GetEnumUnderlyingType().GetScopedCSharpName(scope)); var baseText = @base.Count > 0 ? " : " + string.Join(", ", @base) : string.Empty; sb.Append($"{type.CSharpTypeDeclarationName}{baseText}"); diff --git a/Il2CppInspector.Common/Reflection/TypeInfo.cs b/Il2CppInspector.Common/Reflection/TypeInfo.cs index dd318fb..dfa5186 100644 --- a/Il2CppInspector.Common/Reflection/TypeInfo.cs +++ b/Il2CppInspector.Common/Reflection/TypeInfo.cs @@ -289,7 +289,7 @@ namespace Il2CppInspector.Reflection { } // C#-friendly type name as it should be used in the scope of a given type - public string GetScopedCSharpName(Scope usingScope = null, bool omitRef = false) { + public string GetScopedCSharpName(Scope usingScope = null, bool omitRef = false, bool isPartOfTypeDeclaration = false) { // Unscoped name if no using scope specified if (usingScope == null) return CSharpName; @@ -309,7 +309,13 @@ namespace Il2CppInspector.Reflection { n = n?.Remove(n.IndexOf("`", StringComparison.Ordinal)); // Generic type parameters and type arguments - var g = string.Join(", ", getGenericTypeParameters(usingScope).Select(x => x.GetScopedCSharpName(usingScope))); + // Inheriting from a base class or implementing an interface use the type's declaring scope, not the type's scope itself + // for generic type parameters + var outerScope = usingScope; + if (isPartOfTypeDeclaration) + outerScope = new Scope {Current = usingScope.Current?.DeclaringType, Namespaces = usingScope.Namespaces}; + + var g = string.Join(", ", getGenericTypeParameters(usingScope).Select(x => x.GetScopedCSharpName(outerScope))); if (!string.IsNullOrEmpty(g)) n += "<" + g + ">";