Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow shimming of private methods #11

Open
Miista opened this issue Jan 13, 2024 · 1 comment
Open

Allow shimming of private methods #11

Miista opened this issue Jan 13, 2024 · 1 comment
Labels
enhancement New feature or request priority: low The issue has low priority

Comments

@Miista
Copy link
Owner

Miista commented Jan 13, 2024

Ref: tonerdo/pose#57

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

@Miista Miista added enhancement New feature or request priority: low The issue has low priority labels Jan 17, 2024
@Miista
Copy link
Owner Author

Miista commented Jun 6, 2024

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:

  • Does not use expressions
  • Requires the client to perform the reflection

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority: low The issue has low priority
Projects
None yet
Development

No branches or pull requests

1 participant