简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM