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

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!

.net
reflection
comexception
badimageformatexception
asked on Stack Overflow May 15, 2017 by Florin

2 Answers

0

It does say that it is likely due to a lack of generic context. You can see the difference in their base classes because if you look at the type that System.Collections.Generic.GenericComparer`1 derives from it is not from the base class you said at least when i inspect it. It does not even have a FullName. It also has a GenericTypeArgument[1] whereas t1 does not in my example below.

        var t1 = Type.GetType("System.Collections.Generic.Comparer`1");
        var t2 = Type.GetType("System.Collections.Generic.GenericComparer`1").BaseType;
        bool assignable = t1.IsAssignableFrom(t2);
answered on Stack Overflow May 15, 2017 by cineam mispelt
0

I'm asking if there is a solution for this because I'm using DeepCloner to clone some objects, and some of them have properties that are IEnumerable and their value is an expression that contains an order by or it does a query to the DB.

When I try to clone that kind of object, DeepCloner gives that exception when tries to resolve the constructor of System.Collections.Generic.GenericComparer'1 or when tries to resolve other methods needed for querying the DB.

This is an example in vb.net:

    Public Class cls1
        Public Property prop2 As Integer
    End Class


    Public Class cls0
        Public Property prop1 As IEnumerable(Of cls1)
    End Class

    Private Sub doClone()
        Dim ob1 = New cls0()
        Dim source = New List(Of cls1)
        For i = 0 To 10 - 1
            source.Add(New cls1() With {.prop2 = i})
        Next
        ob1.prop1 = (From a In source Where a.prop2 < 5 Order By a.prop2 Select a)
        ob1.DeepClone()
    End Sub
answered on Stack Overflow May 15, 2017 by Florin • edited May 18, 2017 by Florin

User contributions licensed under CC BY-SA 3.0