1
Vote

ILGenerator reflection issue under Mono

description

When running under Mono, the current release throws a null reference exception at Lib/InternalTypes/Utility.vb:244 when calling GetILGeneratorLength. This is because the Mono implementation of the ILGenerator class doesn't use a member m_length to hold the code length, instead it uses code_len.

I've worked around it by detecting the runtime and choosing the value as appropriate - somewhat of a cludge, but I'm sure someone with more knowledge of this project than me can do better:
Public Shared Function GetILGeneratorLength(ByVal ilg As ILGenerator) As Integer

    Dim fi As System.Reflection.FieldInfo

    If (Type.GetType("Mono.Runtime") Is Nothing) Then
        fi = GetType(ILGenerator).GetField("m_length", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    Else
        fi = GetType(ILGenerator).GetField("code_len", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
    End If

    Return DirectCast(fi.GetValue(ilg), Integer)
End Function

comments