如何隐藏除一次课程以外的所有课程中的数据?

发布于 2024-08-09 04:55:59 字数 587 浏览 7 评论 0原文

我正在尝试修复我最近在某些软件中遇到的设计缺陷,而无需重新编写整个软件。有一个 .exe,它有一个消息侦听器线程,该线程从某个服务器接收数据,然后将其写入单独的 DLL 中的一个类(我们将其称为 StaticDataClass),该类存储它接收到的所有数据(大部分是静态数据)启动时从数据库中提取)。此类还提供其他类可用于检索数据的方法。问题在于存储(设置)方法都是公共的,因此任何其他类都可以覆盖此数据并破坏整个应用程序。对我来说,保护除可执行文件本身包含的对象之外的所有对象的数据(使设置/存储方法不可见)的好方法是什么?

现在我正在玩弄一个仅定义公共 getter 的接口,然后让一个单例类实现该接口,然后 Instance 属性将返回接口类型,因此它们只能使用接口中声明的 get 方法。但是当可执行文件启动时,它将以某种方式获得对 StaticDataClass 对象而不是接口类型的实际引用,因此它也能够调用 store/set 方法。

这并不是完全证明,但它可以防止一些不熟悉系统的开发人员认为可以使用这些公共存储方法,因为 Visual Studio intellitype 使它们在编码时可见。相反,他们必须尝试通过将对象强制转换为包含存储方法的类型来破坏它。

有没有更干净的方法来做到这一点?

I'm trying to fix a design flaw that I recently ran across in some of our software without re-writing the entire thing. There is a .exe which has a message listener thread that is receiving data from some server, and then writing it to a class in a seperate DLL (we'll call it StaticDataClass) that is storing all of the data it receives (mostly static pulled from a database on startup). This class also provides methods that other classes can use to retreive the data. The problem is that the store (set) methods are all public, so any other class can overwrite this data and break the whole application. What is a good way for me to protect the data (make the set/store methods invisible) to all objects except for those contained with the executable itself?

Right now I'm toying with an interface that defines only the public getters, and then having a singleton class implement that interface and then the Instance property will return the interface type so they will only be able to use the get methods declared in the interface. But when the executable starts up, it will somehow get an actual reference to the StaticDataClass object and not the interface type, so it will be able to call the store/set methods as well.

This isn't totally full proof, but it prevents some developer who isn't familiar with the system from think it's ok to use these public storage methods because Visual Studio intellitype makes them visible when they are coding. Instead, they will have to try to break it by casting the object as the type which contains the store methods.

Is there a cleaner way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巡山小妖精 2024-08-16 04:55:59

您可以将设置方法设置为内部方法,并使用InternalsVisibleToAttribute 授予对所需可执行文件的访问权限。

You could make the set methods internal, and use InternalsVisibleToAttribute to grant access to the favoured executable.

南城旧梦 2024-08-16 04:55:59

我同意 - 让内部结构对受信任的程序集可见听起来是实现您想要做的事情的最简单方法。

I agree - making the internals visible to the trusted assembly sounds like the easiest way to achieve what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文