Calling private method in BaseClass from a derived class with Reflection.Emit

2.3k views Asked by At

I'm looking for a way to call a private method from a base class in a derived classes with the TypeBuilder. I know this can be done by simply invoking the method via reflection but attempting to call a passthrough method generated with the typebuilder results in a MethodAccessException. Has anyone found a way around this issue?

1

There are 1 answers

3
Jon Skeet On BEST ANSWER

There is no "issue". You're not allowed to call private methods - that's why they're private. The fact that you can do so with reflection when you've got suitable trust is effectively a backdoor, not an indication that it's something you should be able to do in other ways.

If at all possible you should redesign to avoid the desire to call the private method. If you absolutely have to call it, use reflection - the fact that that's ugly is an indication that you're in an ugly situation.