Implement proper generic parameter substitution

With this patch, generic parameters in BaseType and method param/return
types are substituted correctly and deeply. Next up will be to apply the
same substitution rules to fields, properties, events, ...
This commit is contained in:
Robert Xiao
2020-04-11 22:20:21 -07:00
committed by Katy
parent 202a802274
commit 1970879e52
3 changed files with 44 additions and 11 deletions

View File

@@ -110,14 +110,11 @@ namespace Il2CppInspector.Reflection
DefaultValueMetadataAddress = generic.DefaultValueMetadataAddress;
}
public ParameterInfo SubstituteGenericArguments(TypeInfo[] typeArguments, TypeInfo[] methodArguments) {
/* TODO: Deep substitution */
if (ParameterType.IsGenericTypeParameter)
return new ParameterInfo(this, typeArguments[ParameterType.GenericParameterPosition]);
else if (ParameterType.IsGenericMethodParameter)
return new ParameterInfo(this, methodArguments[ParameterType.GenericParameterPosition]);
else
public ParameterInfo SubstituteGenericArguments(TypeInfo[] typeArguments, TypeInfo[] methodArguments = null) {
TypeInfo t = ParameterType.SubstituteGenericArguments(typeArguments, methodArguments);
if (t == ParameterType)
return this;
return new ParameterInfo(this, t);
}
// ref will be handled as part of the type name

View File

@@ -66,8 +66,7 @@ namespace Il2CppInspector.Reflection {
return Assembly.Model.TypesByReferenceIndex[Definition.parentIndex];
}
if (genericTypeDefinition != null) {
/* TODO substitute generic arguments */
return genericTypeDefinition.BaseType;
return genericTypeDefinition.BaseType.SubstituteGenericArguments(genericArguments);
}
if (Namespace != "System" || BaseName != "Object")
return Assembly.Model.TypesByFullName["System.Object"];
@@ -90,8 +89,7 @@ namespace Il2CppInspector.Reflection {
return type;
}
if (genericTypeDefinition != null) {
/* Generic type instance */
/* TODO substitute generic arguments */
// Generic parameters are *not* substituted in the DeclaringType
return genericTypeDefinition.DeclaringType;
}
return base.DeclaringType;
@@ -264,6 +262,9 @@ namespace Il2CppInspector.Reflection {
n += "*";
return n;
} else {
/* XXX This is not exactly accurate to C# Type.Name:
* Type.Name should be the bare name (with & * [] suffixes)
* but without nested types or generic arguments */
var n = base.Name;
if (DeclaringType != null)
n = DeclaringType.Name + "+" + n;
@@ -832,6 +833,10 @@ namespace Il2CppInspector.Reflection {
// and returns a TypeInfo object representing the resulting constructed type.
// See: https://docs.microsoft.com/en-us/dotnet/api/system.type.makegenerictype?view=netframework-4.8
public TypeInfo MakeGenericType(params TypeInfo[] typeArguments) {
if(typeArguments.Length != genericArguments.Length) {
throw new ArgumentException("The number of generic arguments provided does not match the generic type definition.");
}
TypeInfo result;
if (genericTypeInstances.TryGetValue(typeArguments, out result))
return result;
@@ -840,6 +845,31 @@ namespace Il2CppInspector.Reflection {
return result;
}
public TypeInfo SubstituteGenericArguments(TypeInfo[] typeArguments, TypeInfo[] methodArguments = null) {
if (!ContainsGenericParameters)
return this;
if (IsGenericTypeParameter)
return typeArguments[GenericParameterPosition];
else if (IsGenericMethodParameter)
return methodArguments[GenericParameterPosition];
else if(IsGenericTypeDefinition)
return MakeGenericType(typeArguments);
else if(HasElementType) {
var elementType = ElementType.SubstituteGenericArguments(typeArguments, methodArguments);
if (IsArray)
return elementType.MakeArrayType(GetArrayRank());
else if (IsByRef)
return elementType.MakeByRefType();
else if (IsPointer)
return elementType.MakePointerType();
throw new InvalidOperationException("TypeInfo element type state is invalid!");
} else {
var newGenericArguments = genericArguments.Select(x => x.SubstituteGenericArguments(typeArguments, methodArguments));
return genericTypeDefinition.MakeGenericType(newGenericArguments.ToArray());
}
}
// Initialize a type that is a generic parameter of a generic type
// See: https://docs.microsoft.com/en-us/dotnet/api/system.type.isgenerictype?view=netframework-4.8
public TypeInfo(TypeInfo declaringType, Il2CppGenericParameter param) : base(declaringType) {