views:

97

answers:

4

Take a look at setAccessible - a way in Java to let you call private methods by reflections. Why didn't .NET implement such a feature yet?

+4  A: 

Here is an example of doing that in .NET

Yishai
+2  A: 

I think "why" might be a bit of a speculative question, but if you are looking for a way to test private methods, I think you'll be able to do this easier with .NET 4.0:

http://bugsquash.blogspot.com/2009/05/testing-private-methods-with-c-40.html

mgroves
+3  A: 

You can use reflection to call private methods, though it isn't as straight forward as calling public methods. We've done this before for unit testing and have used the following posts as a how-to reference:

http://www.emadibrahim.com/2008/07/09/unit-test-private-methods-in-visual-studio/

http://johnhann.blogspot.com/2007/04/unit-testing-private-methods.html

James Conigliaro
+1  A: 

It's quite possible in .NET to get access to anything private using reflection.

E.g getting acces to a private instance method called Foo on class bar would look like:

typeof(Bar).GetMethod("Foo",BindingFlags.NonPublic | BindingFlags.Instance);

it does however require that the code using reflection is fully trusted.

(The security requirements will be different for V4)

Rune FS