且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在同一解决方案中访问主表单

更新时间:2023-11-22 19:33:22

不要。



即使尝试也存在问题,例如循环引用:因为你的主表单需要引用你的类库(为了访问你的人才代码,你不能在不设置循环引用的情况下将你的Main项目的引用添加到你的talent项目中,这意味着你的项目永远无法编译(因为对Main项目的任何更改)意味着人才需要重建,这导致主要需要重建,这意味着人才需要重建,等等。



从逻辑上讲,它错了同样 - 你的DLL根本不需要知道关于表示层的任何内容!如果有,你不能在以后更换图层,这是一个坏主意。



相反,请查看在表单处理的库中创建事件,以便在需要时向库提供信息。
Don't.

There are problems with even trying, such as circular references: since your Main form needs to reference your class library (in order to access your "talent" code) you can't add a reference to your Main project to your "talent" project without setting up a circular reference which would mean that your project could never compile (as any change to the Main project means the talent needs to be rebuilt, which causes the main to need rebuilding, which means that talent needs to be rebuilt, and so on.

And logically, it wrong as well - your DLL shouldn't need to know anything about the presentation layer at all! If it does, you can't replace a layer at a later date, which is a bad idea.

Instead, look at creating events in the library which the form handles to provide information to the library when it needs it.