views:

63

answers:

1

In .NET, are private methods and properties enforced by the runtime or just by the compiler?

If you try to call another object's private methods, the compiler will throw an access exception. What if you manually manipulate the IL or try to call via reflection -- will you be able to? Also, does it vary by runtime version (1.1 vs. 2.0 vs. 3.5 vs. 4.0)?

+4  A: 

It is a compile time restriction. You can call private methods via Reflection in all versions of .NET. And starting in .NET 4.0, you can use dynamics to do it. In addition, using Reflection, you can see and use the methods behind automatic properties. It should go without saying that this isn't recommended.

Yuriy Faktorovich