Consider the following code:

```
private class ThirdPartyClass {
    private class InternalPrivateClass { }
    private static InternalPrivateClass Init() { 
        return new InternalPrivateClass(); 
    }
    private static int DoSomething(InternalPrivateClass t1) { 
        return 0; 
    }
}

```

Assume I have no control over `ThirdPartyClass` and reverse-engineering it in any way is cost-prohibitive. I want to be able to quickly call `DoSomething` without the performance overheads of reflection. So what I have so far:

```
Type t = typeof(ThirdPartyClass);
object context = t.GetMethod("Init", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null);
MethodInfo mi = t.GetMethod("DoSomething", BindingFlags.NonPublic | BindingFlags.Static);
// ...now what?

```

* Calling `mi.Invoke(null, new object[]{context})` is of course slow, because it's using reflection.
* `Delegate.CreateDelegate(typeof(Func<object, int>), mi);`fails because the Func signature must match exactly and (`object, int`) doesn't match the MethodInfo's signature (`ThirdPartyClass.InternalPrivateClass, int`)
* Constructing a properly typed Delegate via reflection (see <https://stackoverflow.com/a/40579063/2692950>) only lets me call `.DynamicInvoke(context)` which is still slow. I cannot cast this delegate to a Func to be able to invoke it directly because, again, the signatures don't match.
* I cannot write `Func<ThirdPartyClass.InternalPrivateClass, int>` - it won't compile since `InternalPrivateClass` is private.

*Solved! (<https://stackoverflow.com/a/52652398/2692950>)*

ᅟ

**Example of why I need this:**

Take a look at this MD4 hash implementation: <https://stackoverflow.com/a/46821287/2692950>
(Shortened version: <https://stackoverflow.com/a/52640221/2692950>)

This works very well, except that every single hashing operation is calling a method via reflection!

In this example, we call via reflection an inaccessible private function `System.Security.Cryptography.Utils.HashEnd(SafeProvHandle h)`, passing a `SafeHandle` as the parameter. This works because `SafeProvHandle` inherits from `SafeHandle`. `SafeProvHandle` cannot be referenced directly because it's private, hence there appears to be no way invoke this function directly.

*(I'm mostly interested in whether a solution exists to the general case at the top of the question, but if anyone knows of a better way to implement obtaining a crypto service provider directly by `ALG_ID`, I'm all ears :)*