lundi 15 mai 2017

System.BadImageFormatException when trying to resolve constructor of System.Collections.Generic.GenericComparer`1

I have a problem with the following code:

var type1 = typeof(object);
var type2 = type1.Module.GetType("System.Collections.Generic.GenericComparer`1");
var constr = type2.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
var byteArray = constr.GetMethodBody().GetILAsByteArray();
var result = type2.Module.ResolveMethod(BitConverter.ToInt32(byteArray, 2));

Everytime I execute it it gives me the following error:

An exception of type 'System.BadImageFormatException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary
Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

However, if insteaf of

var type2 = type1.Module.GetType("System.Collections.Generic.GenericComparer`1");

I use its base class

var type2 = type1.Module.GetType("System.Collections.Generic.Comparer`1");

then the "ResolveMethod" returns OK.

Does anybody know why that class can't be "resolved"?

Thank you!





Aucun commentaire:

Enregistrer un commentaire