Add method to replace generic params with generic args.

The code only handles basic cases, eg. T -> int, but not T[] -> int[].
This commit is contained in:
de4dot 2011-11-03 22:53:04 +01:00
parent b810292cee
commit c1820f1a82

View File

@ -782,5 +782,26 @@ namespace de4dot.blocks {
return Instruction.Create(OpCodes.Ldc_I4_S, (sbyte)value);
return Instruction.Create(OpCodes.Ldc_I4, value);
}
// Doesn't fix everything (eg. T[] aren't replaced with eg. int[], but T -> int will be fixed)
public static IList<TypeReference> replaceGenericParameters(GenericInstanceType typeOwner, GenericInstanceMethod methodOwner, IList<TypeReference> types) {
for (int i = 0; i < types.Count; i++)
types[i] = getGenericArgument(typeOwner, methodOwner, types[i]);
return types;
}
public static TypeReference getGenericArgument(GenericInstanceType typeOwner, GenericInstanceMethod methodOwner, TypeReference type) {
var gp = type as GenericParameter;
if (gp == null)
return type;
if (typeOwner != null && MemberReferenceHelper.compareTypes(typeOwner.ElementType, gp.Owner as TypeReference))
return typeOwner.GenericArguments[gp.Position];
if (methodOwner != null && MemberReferenceHelper.compareMethodReferenceAndDeclaringType(methodOwner.ElementMethod, gp.Owner as MethodReference))
return methodOwner.GenericArguments[gp.Position];
return type;
}
}
}