CNeptune
CNeptune copied to clipboard
Problem with BadImageFormatException when instantiating generic class referencing ObservableCollection
The following code will reproduce the problem Using CNeptune version 1.2.1
namespace NConcernConstructorFail
{
class Program
{
static void Main(string[] args)
{
var a = new A(); // OK
var b = new B<int>(); // System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'
}
}
class A
{
ObservableCollection<int> o = new ObservableCollection<int>();
}
class B<T>
{
ObservableCollection<T> o = new ObservableCollection<T>();
}
}
Actually not related to ObservableCollection<T>
also happens with List<T>
Interesting, thank you for report. I will try it as soon as possible.
FWIW: I tryed decompile with ILSpy, after fixing a null reference error in ILSpy I got the following for B<T>
internal class B<T>
{
[CompilerGenerated, Serializable]
internal static class <Neptune>
{
[CompilerGenerated, Serializable]
public static class <Authentic>
{
public static void <Constructor>(B @this) // Should be B<T> not B (probably not ILSpy issue, manual written similar code decompile ok)
{
@this.o = new List<!0>(); // Should be T not !0 (probably not ILSpy issue, manual written similar code decompile ok)
@this..ctor(); // OK, ILSpy is confused code: actually @object..ctor(@this);
}
}
[CompilerGenerated, Serializable]
public static class <Intermediate>
{
[CompilerGenerated, Serializable]
public static class <<Constructor>>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated]
public static IntPtr <Pointer> = MethodBase.GetMethodFromHandle(methodof(B.<Neptune>.<Authentic>.<Constructor>(B)).MethodHandle, typeof(B.<Neptune>.<Authentic>).TypeHandle).MethodHandle.GetFunctionPointer(); // not sure probably should have been B<T>
}
}
}
private List<T> o;
public B()
{
calli(System.Void(NConcernConstructorFail.B`1), this, B.<Neptune>.<Intermediate>.<<Constructor>>.<Pointer>); // not sure probably should have been B<T>
}
I found a hint,the generated delegates lost generic type constraints. I'm trying on it
Same error - was this fixed?
This issue is still there. For checking if the issue was with Mono.Cecil, I upgraded Mono.Cecil to 10.0 and changed some breaking code within CNeptune. But it did not help resolve this issue.
I fixed it on my branches and made a pull request however Virtuoze has not taken the pull request. You can download the source from my branches https://github.com/jho1965us/CNeptune and https://github.com/jho1965us/NConcern but you will have to build it your self. Also the code that modifies the project file to run NConcern is not on github so you will have to modify the project file manually (Hint install the official version first then modify the project to the CNeptune.exe you build your self). More details in the the pull request comment https://github.com/Virtuoze/CNeptune/pull/17