【发布时间】:2017-05-15 09:16:45
【问题描述】:
我对以下代码有疑问:
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));
每次我执行它都会给我以下错误:
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)
但是,如果不这样做
var type2 = type1.Module.GetType("System.Collections.Generic.GenericComparer`1");
我使用它的基类
var type2 = type1.Module.GetType("System.Collections.Generic.Comparer`1");
然后“ResolveMethod”返回OK。
有人知道为什么这个类不能被“解决”吗?
谢谢!
【问题讨论】:
-
我尝试在 32 位和 64 位中构建应用程序,但都给出了错误。我认为这与此无关。
-
可能是因为
GenericComparer是内部的? -
不这么认为..我已经尝试过 System.Collections.StructuralComparer,它也是内部的,它可以工作..
标签: .net reflection comexception badimageformatexception