为什么是它甚至可能改变一个私有成员,或在C#中使用反射运行一个私有方法? [英] Why is it even possible to change a private member, or run a private method in C# using reflection?

查看:131
本文介绍了为什么是它甚至可能改变一个私有成员,或在C#中使用反射运行一个私有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到了,我用C#有一个问题,它被设置使用私有成员解决反射

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

我吃惊地发现,设置私有成员/场,并运行一个私有方法事情被允许和可能在C#中。这不是如何做这些事情的问题,他们是有据可查的,我的问题是:为什么?

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?

如果您设置字段/会员/方法,私有/内部,为什么会C#作为一种语言允许这些字段被设置范围之外?我认为,这将抛出某种异常。如果类希望他们改变或组不会有是一个方法或构造?

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?

推荐答案

由于访问修饰符在那里协助记录你的的API想的暴露给消费者,或者继承人等。

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.

这篇关于为什么是它甚至可能改变一个私有成员,或在C#中使用反射运行一个私有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆