Miista / pose

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

Allow shimming of private methods #11

Open Miista opened 6 months ago

Miista commented 6 months ago

Ref: https://github.com/tonerdo/pose/issues/57

Note that this would be incompatible with the changes in #13. Or at the very least it would introduce an alternate API.

Miista commented 1 month ago

This would indeed be possible by adding the following code to Shim.

public static Shim UnsafeReplace(MethodInfo method, bool setter = false)
{
    return new Shim(method, method.DeclaringType) { _setter = setter };
}

The above supports both methods and properties. The UnsafeReplace method would be used as follows:

var methodInfo = typeof(Class).GetMethod("PrivateMethod", BindingFlags.Instance | BindingFlags.NonPublic);
var with = Shim.UnsafeReplace(methodInfo).With((Class @this) => 100);

Please note, however, that this:

I cannot see a good reason for shimming private members. That is not to say that it won't be implemented.