tonerdo / pose

Replace any .NET method (including static and non-virtual) with a delegate
MIT License
1.07k stars 75 forks source link

Can't use Console.WriteLine in PoseContext #43

Open asherber opened 5 years ago

asherber commented 5 years ago

I'm not sure if this is related to #14 and #15, or if it's maybe just user error.

I was taking Pose for a quick test drive and created a shim for DateTime.Now and then tried to output it, but the app threw an exception. Then I took out any use of the shim from within the isolation, and I'm still getting an error. IOW, it seems that just calling Console.WriteLine() from within an isolation context throws an exception.

Sample code:

static void Main(string[] args)
{
    var shim = Shim.Replace(() => DateTime.Now).With(() => DateTime.Parse("2000-01-01"));

    PoseContext.Isolate(() =>
    {
        // originally had Console.WriteLine(DateTime.Now)
        Console.WriteLine("foo");
    }, shim);

    Console.ReadLine();
}

Exception:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidProgramException: JIT Compiler encountered an internal limitation.
   at dynamic_System.Runtime.ConstrainedExecution.CriticalFinalizerObject_.ctor(CriticalFinalizerObject )
   at stub_ctor_System.Runtime.ConstrainedExecution.CriticalFinalizerObject_.ctor(CriticalFinalizerObject , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_System.Runtime.InteropServices.SafeHandle_.ctor(SafeHandle , IntPtr , Boolean )
   at stub_ctor_System.Runtime.InteropServices.SafeHandle_.ctor(SafeHandle , IntPtr , Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid_.ctor(SafeHandleZeroOrMinusOneIsInvalid , Boolean )
   at stub_ctor_Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid_.ctor(SafeHandleZeroOrMinusOneIsInvalid , Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_Microsoft.Win32.SafeHandles.SafeFileHandle_.ctor(SafeFileHandle , IntPtr , Boolean )
   at stub_ctor_Microsoft.Win32.SafeHandles.SafeFileHandle_.ctor(IntPtr , Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_System.Console_GetStandardFile(Int32 , FileAccess , Int32 )
   at stub_System.Console_GetStandardFile(Int32 , FileAccess , Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_System.Console_OpenStandardOutput(Int32 )
   at stub_System.Console_OpenStandardOutput(Int32 , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_System.Console_InitializeStdOutError(Boolean )
   at stub_System.Console_InitializeStdOutError(Boolean , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_System.Console_get_Out()
   at stub_System.Console_get_Out(RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_System.Console_WriteLine(String )
   at stub_System.Console_WriteLine(String , RuntimeMethodHandle , RuntimeTypeHandle )
   at dynamic_PoseTest.Program+<>c_<Main>b__0_2(<>c )
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at Pose.PoseContext.Isolate(Action entryPoint, Shim[] shims)
   at PoseTest.Program.Main(String[] args) in C:\Users\Aaron\Documents\Projects\Visual Studio\Pose\Pose\Program.cs:line 16