Consider the following code:
private class ThirdPartyClass {
private class InternalPrivateClass { }
private static int SomeFunc(InternalPrivateClass t1) {
return 0;
}
}
I have no control over ThirdPartyClass
. I want to be able to quickly call SomeFunc
without the performance overheads of reflection. So what I have so far:
Type t = typeof(ThirdPartyClass);
MethodInfo mi = t.GetMethod("SomeFunc", BindingFlags.NonPublic | BindingFlags.Static);
// ...now what?
- Calling
mi.Invoke()
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()
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 sinceInternalPrivateClass
is private.
ᅟ
Why I need this:
Take a look at this MD4 hash implementation: https://stackoverflow.com/a/46821287/2692950
This works very well, except that every single hashing operation is calling a method via reflection!
(If anyone knows of a better way to implement obtaining a crypto service provider directly by ALG_ID
, I'm all ears :)
Aucun commentaire:
Enregistrer un commentaire