question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importer create ClassSig rather than GenericInstSig for generic type

See original GitHub issue

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) { }
}

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
wwh1004commented, May 22, 2022

In old commit https://github.com/0xd4d/dnlib/commit/4ebebc1cd335676158688cc2e786de16b152f173#diff-3bc1d65c9144274297f0e9a1b2e194898aa3aa9e441d2956ab303ab4522dc554L488 code is

var fieldSig = new FieldSig(ImportAsTypeSig(origField.FieldType));

So I use “origField” instead of “fieldInfo”. But after running more tests I find “fieldInfo” is true.
Also I find more problems about generic like nested case of “MustTreatTypeAsGenericInstType”, wrong result of “FieldComparer.Equals”, inconsistent result of GetHashCode(SRM.FieldInfo) and GetHashCode(DNN.IField), ContainsGenericParameter for generic MemberRef always return true.
I will fix them these days.

0reactions
wtfsckcommented, May 22, 2022

PRs welcome. I don’t have time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Export a concrete version of a generic TypeScript class
Somehow the following works in TypeScript 4.6.3 : //specificthing.ts import { GenericThing } from "./genericthing"; export class ThingOwner ...
Read more >
feat request: Ability to import generic type functions without ...
feat request: Ability to import generic type functions without duplicating the parameters. #32055.
Read more >
Writing R Extensions
A package or ' R ' can appear more than once in the ' Depends ' field, for example to give upper and...
Read more >
in (Generic Modifier) - C# Reference
For generic type parameters, the in keyword specifies that the type parameter is contravariant. You can use the in keyword in generic interfaces ......
Read more >
(PDF) Datatype-generic programming | Jeremy Gibbons
A generic program is written once and works on values of many data types. Generic Haskell is a recent extension of the functional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found