CS: Fix "Namespace used like type" (CS0118) (needs additional work)

This commit is contained in:
Katy Coe
2020-06-22 22:35:32 +02:00
parent ef22c6628b
commit 42310483af
2 changed files with 27 additions and 0 deletions

View File

@@ -16,6 +16,9 @@ namespace Il2CppInspector.Reflection
public Il2CppInspector Package { get; } public Il2CppInspector Package { get; }
public List<Assembly> Assemblies { get; } = new List<Assembly>(); public List<Assembly> Assemblies { get; } = new List<Assembly>();
// List of all namespaces defined by the application
public List<string> Namespaces { get; }
// List of all types from TypeDefs ordered by their TypeDefinitionIndex // List of all types from TypeDefs ordered by their TypeDefinitionIndex
public TypeInfo[] TypesByDefinitionIndex { get; } public TypeInfo[] TypesByDefinitionIndex { get; }
@@ -113,6 +116,9 @@ namespace Il2CppInspector.Reflection
GenericMethods[spec] = method; GenericMethods[spec] = method;
} }
// Generate a list of all namespaces used
Namespaces = Assemblies.SelectMany(x => x.DefinedTypes).GroupBy(t => t.Namespace).Select(n => n.Key).Distinct().ToList();
// Find all custom attribute generators (populate AttributesByIndices) (use ToList() to force evaluation) // Find all custom attribute generators (populate AttributesByIndices) (use ToList() to force evaluation)
var allAssemblyAttributes = Assemblies.Select(a => a.CustomAttributes).ToList(); var allAssemblyAttributes = Assemblies.Select(a => a.CustomAttributes).ToList();
var allTypeAttributes = TypesByDefinitionIndex.Select(t => t.CustomAttributes).ToList(); var allTypeAttributes = TypesByDefinitionIndex.Select(t => t.CustomAttributes).ToList();

View File

@@ -524,6 +524,27 @@ namespace Il2CppInspector.Reflection
if (matchingNamespaces.Count == 0 && !hidden && string.IsNullOrEmpty(mutualRootScope) && usingDirective == null) if (matchingNamespaces.Count == 0 && !hidden && string.IsNullOrEmpty(mutualRootScope) && usingDirective == null)
minimallyScopedName = usedType; minimallyScopedName = usedType;
} }
// Finally, check if the selected name has ambiguity with any available namespaces in the current scope
// If so, use the full name with the mutual root scope cut off from the start
var checkNamespaces = scope.Namespaces.Select(ns => (!string.IsNullOrEmpty(ns)? ns + "." : "") + minimallyScopedName).ToList();
if (Assembly.Model.Namespaces.Intersect(checkNamespaces).Any())
minimallyScopedName = mutualRootScope.Length > 0 ? usedType.Substring(mutualRootScope.Length + 1) : usedType;
// Check current namespace and all ancestors too
else {
checkNamespaces.Clear();
var ancestorUsingScope = "." + usingScope;
while (ancestorUsingScope.IndexOf(".", StringComparison.Ordinal) != -1) {
ancestorUsingScope = ancestorUsingScope.Substring(0, ancestorUsingScope.LastIndexOf(".", StringComparison.Ordinal));
checkNamespaces.Add((ancestorUsingScope.Length > 0 ? ancestorUsingScope.Substring(1) + "." : "") + minimallyScopedName);
}
if (Assembly.Model.Namespaces.Intersect(checkNamespaces).Any())
minimallyScopedName = mutualRootScope.Length > 0 ? usedType.Substring(mutualRootScope.Length + 1) : "global::" + usedType;
}
return minimallyScopedName; return minimallyScopedName;
} }