Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a private method with Reflection

Is it possible to override a private method by using Reflection in .NET 3.5?

like image 588
dr. evil Avatar asked Sep 20 '09 19:09

dr. evil


People also ask

Can we access private methods using reflection?

If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.

How do you override a private method?

No, we cannot override private or static methods in Java. Private methods in Java are not visible to any other class which limits their scope to the class in which they are declared.

Is it possible to override a private method in Java?

2) In Java, methods declared as private can never be overridden, they are in-fact bounded during compile time.

Can we call private method using reflection C#?

Answer: To access private method in C# programming of a class, we need to use reflection technique.


1 Answers

Well, it would need to be virtual to be possible to override it (by writing a dynamic type that inherits from the class), and you can't have a private virtual (it makes no sense). You could perhaps override an internal virtual, but I suspect even this may hit security issues. So ultimately, I'd say no.

like image 112
Marc Gravell Avatar answered Sep 20 '22 05:09

Marc Gravell