CNeptune icon indicating copy to clipboard operation
CNeptune copied to clipboard

Problem with BadImageFormatException when instantiating generic class referencing ObservableCollection

Open jho1965us opened this issue 7 years ago • 7 comments

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>();
    }
}

jho1965us avatar Jun 10 '17 18:06 jho1965us

Actually not related to ObservableCollection<T> also happens with List<T>

jho1965us avatar Jun 10 '17 18:06 jho1965us

Interesting, thank you for report. I will try it as soon as possible.

Puresharper avatar Jun 12 '17 09:06 Puresharper

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>
	}

jho1965us avatar Jun 12 '17 11:06 jho1965us

I found a hint,the generated delegates lost generic type constraints. I'm trying on it

hoxcape avatar Aug 25 '17 12:08 hoxcape

Same error - was this fixed?

akovac35 avatar Feb 20 '18 13:02 akovac35

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.

asheeshb avatar Jul 24 '18 11:07 asheeshb

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

jho1965us avatar Jul 24 '18 13:07 jho1965us