System.InvalidProgramException : Common Language Runtime detected an invalid program. Seeing this error message whilst trying to check the expression's correctness and if it is the correct IL.
The expression is "DateTimeA.GetType().Name", where we expect the result to be a string i.e. "String;DateTimeA.GetType().Name;String".
This is an example from "ValidExpressions.txt" using mparlak's "Flee" (see https://github.com/mparlak/Flee/blob/master/src/Flee/).
For some reason, the code is failing on that specific expression. I've checked what the emitted ops are and it goes as follows...
ldarg.0
ldfld
callvirt
callvirt
ret
length 17
I've run the test on the Flee library (as the .txt file was in the tests but the test was previously removed), and it fails with this error. Not entirely sure how to resolve it... I expect this test to pass with no problems but it appears that the "GetType()" method specifically is causing problems and I'm not sure why...
FYI, this is using .NET Standard, and there was an interesting note about this for GetType()
since the Save
option for the AssemblyBuilderAccessor
was removed... and I found an interesting excerpt from Microsoft, but not entirely sure if this is relevant to the issue.
GetType only works on assemblies loaded from disk. If you call GetType to look up a type defined in a dynamic assembly defined using the System.Reflection.Emit services, you might get inconsistent behavior. The behavior depends on whether the dynamic assembly is persistent, that is, created using the RunAndSave or Save access modes of the System.Reflection.Emit.AssemblyBuilderAccess enumeration. If the dynamic assembly is persistent and has been written to disk before GetType is called, the loader finds the saved assembly on disk, loads that assembly, and retrieves the type from that assembly. If the assembly has not been saved to disk when GetType is called, the method returns null. GetType does not understand transient dynamic assemblies; therefore, calling GetType to retrieve a type in a transient dynamic assembly returns null.
To use GetType on a dynamic module, subscribe to the AppDomain.AssemblyResolve event and call GetType before saving. Otherwise, you will get two copies of the assembly in memory.