Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it even possible to change a private member, or run a private method in C# using reflection? [duplicate]

I recently came across a problem that I was having using C#, and it was solved by setting a private member using reflection.

I was stunned to find out that setting a private member/field and also running a private method are things that are allowed and possible in C#. This is not a question of how to do these things, they are well documented, my question is: why?

If you set a field/member/method as private/internal, why would C# as a language allow these fields to be set outside the scope? I would think that this would throw an exception of some kind. If the class wanted them to be changed or set wouldn't there be a method or a constructor?

like image 320
Ryan Drost Avatar asked Jun 05 '13 14:06

Ryan Drost


People also ask

What is the purpose of a private method?

Private methods are useful for breaking tasks up into smaller parts, or for preventing duplication of code which is needed often by other methods in a class, but should not be called outside of that class.

Can a private method call another private method C#?

You don't. The point of making it private is so your compiler can notify you that you cannot call it from another class. If you want to call it from the same assembly, make it internal . If you want to call it from a different assembly, make it public .

What happens if a method is private?

Private methods can be called only inside the class. You can call public methods of your class anywhere in program. Methods without access modifier are meant to have package visibility scope (it's called default), so you can invoke it anywhere in package, where class is defined.

When should a member function be private?

8 Answers. Show activity on this post. You should make a function private when you don't need other objects or classes to access the function, when you'll be invoking it from within the class. Stick to the principle of least privilege, only allow access to variables/functions that are absolutely necessary.

What are private methods in C++?

In some modern highly object oriented languages private methods exist only by convention. Method with '_' on beginning is considere to be private. Show activity on this post. Private methods are those methods which can’t be accessed in other class except the class in which they are declared.

How do you convert a public method to a private method?

Create a new public method, copy the body of the original method to it, and replace the local variables with the private fields of your new class. Call the method from the new class in the original class. Let’s say you have the following God class and the private method IsLifeBeautiful.

What is the use of private variables in Java?

In Java, private variables are visible to the whole class. They can be accessed from static methods and from other instances of the same class. This is, for example, useful in factory methods. A factory method usually does initializations to an object which are so complex that you do not want to leave them to the application code.

How does the public/protected/private mechanism work in C++?

The public/protected/private mechanism works only on names (so the name info is inaccessible for outsiders), but if you can get your hands on a pointer/reference to a private member, you have full access to that member. Show activity on this post. Access descriptors in C++ are a static mechanism.


1 Answers

Because the access modifiers are there to assist with documenting the API that you want to expose to consumers, or to inheritors, etc.

They're not a security/access control mechanism.

like image 97
Damien_The_Unbeliever Avatar answered Sep 28 '22 12:09

Damien_The_Unbeliever