tonerdo / pose

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

Isolated call with linq method throws exception #11

Open ilengyel opened 6 years ago

ilengyel commented 6 years ago

Hello,

I get the following exception with version 1.2.0 of pose while shimming a method which has an extension method. This worked in v1.0.1

PoseTest.Test.TestShim3 [FAIL]
  System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace:
       at Pose.Helpers.StubHelper.GetIndexOfMatchingShim(MethodBase methodBase, Type type, Object obj)
       at stub_virt_System.Collections.Generic.ICollection`1[System.String]_get_Count(ICollection`1 , RuntimeMethodHandle , RuntimeTypeHandle )
       at dynamic_System.Linq.Enumerable_LastOrDefault(IEnumerable`1 )
       at stub_System.Linq.Enumerable_LastOrDefault(IEnumerable`1 , RuntimeMethodHandle , RuntimeTypeHandle )
       at dynamic_PoseTest.Program_LinqFn(Program )
       at stub_virt_PoseTest.Program_LinqFn(Program , RuntimeMethodHandle , RuntimeTypeHandle )
       at dynamic_PoseTest.Test+<>c__DisplayClass2_0_<TestShim3>b__1(<>c__DisplayClass2_0 )

Sample project: PoseTest.zip

code extract:

var target = new Program();
var shim = Shim.Replace(() => target.Work())
    .With((Program @this) => "Changed");
PoseContext.Isolate(() => work = target.LinqFn(), shim);

// ...
public string LinqFn()
{
    var items = new[] { Work() };
    var item = items.LastOrDefault();
    return item;
}
ilengyel commented 6 years ago

Also I attempted to compile the pose solution but when I do dotnet restore I get the following error

  Installing Mono.Reflection 1.1.0.0.
C:\Program Files\dotnet\sdk\1.0.4\NuGet.targets(97,5): error : Package Mono.Reflection 1.1.0 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package Mono.Reflection 1.1.0 supports: net (.NETFramework,Version=v0.0) [C:\DEV\INVESTIGATE\GIT\pose\Pose.sln]
C:\Program Files\dotnet\sdk\1.0.4\NuGet.targets(97,5): error : One or more packages are incompatible with .NETStandard,Version=v2.0. [C:\DEV\INVESTIGATE\GIT\pose\Pose.sln]

My environment is VS2017 (15.4.3). Any dependencies I need to install?

tonerdo commented 6 years ago

Also I attempted to compile the pose solution but when I do dotnet restore I get the following error

You probably have a setting to treat warnings as errors turned on somewhere

tonerdo commented 6 years ago

I get the following exception with version 1.2.0 of pose while shimming a method which has an extension method. This worked in v1.0.1

Thanks for the heads up and for providing sample code. I'll take a look at it and get back to you