Skip to content

Importer create ClassSig rather than GenericInstSig for generic type #462

@CreateAndInject

Description

@CreateAndInject

I don't know if it's a .NET runtime bug or dnlib bug.
.NET runtime indicate different IsGenericTypeDefinition for the same type Task<>
dnlib create ClassSig since IsGenericType and IsGenericTypeDefinition are both True;

			if (a.IsGenericType && !a.IsGenericTypeDefinition)
				return ElementType.GenericInst;
using System;
using System.Reflection;
using System.Threading.Tasks;
using dnlib.DotNet;

class Program {
	static void Main() {
		var methodInfo1 = typeof(Task<string>).GetMethod("ContinueWith", new Type[] { typeof(Action<Task<string>>) });
		Show(methodInfo1); // True

		var methodInfo2 = typeof(Demo2<string>).GetMethod("Test");
		Show(methodInfo2); // False

		var moule = ModuleDefMD.Load(typeof(Program).Module);
		var method1 = moule.Import(methodInfo1); // System.Threading.Tasks.Task System.Threading.Tasks.Task`1<System.String>::ContinueWith(System.Action`1<System.Threading.Tasks.Task`1>)
		Show(method1); // Class
		var method2 = moule.Import(methodInfo2); // System.Void Demo2`1<System.String>::Test(System.Action`1<System.Threading.Tasks.Task`1<System.String>>)
		Show(method2); // GenericInst
		Console.ReadKey();
	}

	static void Show(MethodInfo method) {
		var origMethod = method.Module.ResolveMethod(method.MetadataToken);
		var type = origMethod.GetParameters()[0].ParameterType.GenericTypeArguments[0];
		Console.WriteLine(type.IsGenericTypeDefinition);
	}

	static void Show(IMethod method) {
		var typeSig = (method.MethodSig.Params[0] as GenericInstSig).GenericArguments[0];
		Console.WriteLine(typeSig.ElementType);
	}
}

class Demo2<T> {
	public void Test(Action<Task<T>> arg) { }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions