pose icon indicating copy to clipboard operation
pose copied to clipboard

Offer to make public Shim constructor

Open InsonusK opened this issue 6 years ago • 1 comments

Hello, I am using your framework.

I have meet such situation: I need to Shim private static method. But your framework could not do it, because Shim.Replace could not understande reflection.

But I have found solution: var _newShim = (Shim) typeof(Shim) .GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] {typeof(MethodBase), typeof(object)}, null) .Invoke( new object[] { typeof(TestClass) .GetMethods(BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod) .Single(m => m.Name == privateMethodName), null }); _newShim.With(delegate(int val) { return val + 1; });

It will be much easy if Shim have got public constructor.

InsonusK avatar Dec 08 '19 18:12 InsonusK

@InsonusK In the current implementation it's only possible to shim public members. I understand you would like the option to shim private (and possibly also internal and protected) members.

In its current form, the library uses expressions to resolve the method/property. This gives us a strongly typed way to verify the method-to-shim and its replacement.

Non-public members cannot (by definition) be resolved via expressions. We would therefore have to give up some of the strong typing to support this.

Miista avatar Jan 13 '24 12:01 Miista