Visual Studio 2008 可执行文件和程序集的项目组织
我在 Visual Studio 2008 中设置以下内容时遇到问题:包含入口点 Main() 方法类并声明接口的父项目,以及具有实现父项目中声明的接口的类的子项目。
我已指定父级的输出类型是控制台应用程序,子级的输出类型是类库。在 Child 中,我添加了对 Parent 的引用作为项目,并指定 Child 依赖于 Parent,并且构建顺序应该是 Parent,然后是 Child。
构建成功,据我所知,正确的内容出现在 Child/bin/debug 目录中:Parent.exe 和 Child.dll。
但是,如果我运行 Parent.exe,那么当它应该从 Child.dll 加载类时,它会失败并显示错误消息:
异常执行操作 System.TypeLoadException:无法加载类型“Child.some.class”来自程序集“Parent,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”。
我想我对如何让父项目和子项目一起玩感到困惑。我计划让更多的子项目使用在父项目中设置的相同框架,因此我不想将入口点类移到子项目中。如果我尝试指定子项目也是控制台应用程序,则构建过程会失败,因为子项目中没有 Main() 入口点类(即使包含父项目作为引用)。
欢迎任何帮助!谢谢,马丁
I am having a problem setting up the following in Visual Studio 2008: a parent project which includes the entrypoint Main() method class and which declares an interface, and a child project which has classes that implement the interface declared in the parent project.
I have specified that Parent's Output type is a Console application, and Child's Output type is a Class library. In Child I have add a reference to the Parent as a project, and specified that Child depends on Parent and that the build order should be Parent, then Child.
The build succeeds, and as far I can tell, the right things show up in the Child/bin/debug directory: Parent.exe and Child.dll.
However, if I run Parent.exe, then at the point when it should load a class from the Child.dll, it fails with the error message:
exception executing operation System.TypeLoadException: Could not load type 'Child.some.class' from assembly 'Parent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
I guess I'm confused as to how to get the Parent and Child projects to play together. I plan on having more child projects that use the same framework that is set up in the Parent, and so I do not want to move the entrypoint class down into the Child project. If I try to specify that the Child project is also a Console application, then the build process fails because there is no Main() entrypoint class in the child (even though the Parent project is included as a reference).
Any help would be welcome! Thanks, Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,按照回答自己问题的优良传统,我发现为了成功调用
GetType()
,我必须指定正确的程序集。每个项目都编译成自己的程序集(Application.exe
、Interface.dll
和Implementation.dll
)。因此,为了在Application Main()
类中实例化实现类,我必须调用:看起来有点做作,但至少它有效。
So in the fine tradition of answering one's own questions, I worked out that in order to call
GetType()
successfully, I have to specify the correct assembly. Each project is compiled into its own assembly (Application.exe
,Interface.dll
, andImplementation.dll
). So in order to instantiate the implementation class within theApplication Main()
class, I have to call:It seems a little hokey, but at least it works.
您希望避免循环依赖。
如果您希望将接口和实现分开,则在“子”程序集(类库)使用的第三个程序集(类库)中定义接口,然后使用应用程序中的接口和实现。
You want to avoid circular dependencies.
If you wish to keep the interface and implementasiton separate, then define the interface in a third assembly (class library) that is used by the "child" assembly (class library), then use both the interface and implementation from the application.
一般来说,您想要反转您的层次结构:您的 .exe 项目应该依赖于您的卫星程序集项目(您称之为“子项目”)。然后,要使用附属程序集中的类型,您需要将 .exe 项目中的引用添加到程序集项目中。为此,请右键单击 VS 解决方案资源管理器中的 .exe 项目,然后执行“添加引用...”,单击“项目”选项卡并选择程序集项目。
编辑:
如果您想为整个项目设置一个“框架”(通过定义 接口或基类),那么我首先在单个核心程序集中这样做;将这些定义放入 MyProject.Core.dll 之类的文件中。然后,您的“实现”项目/程序集将引用您的 MyProject.Core 项目。综上所述,您可能正在寻找类似 IOC / DI 框架的东西。请参阅 Ninject、StructureMap、温莎城堡等
Generally you want to reverse your hierarchy: your .exe project should depend on your satellite assembly projects (what you're calling "Child"). Then, to use the types in your satellite assembly, you want to add a reference from your .exe project to your assembly project. Do this by right-clicking the .exe project in the Solution Explorer of VS, then do Add Reference ... click the Projects tab and choose the assembly project.
Edit:
If you want to setup a "framework" for the overall project (either by defining interfaces or base classes) then I'd start by doing so in a single, core assembly; put those definitions in something like MyProject.Core.dll. Then your "implementing" projects / assemblies would have a reference to your MyProject.Core project. With all this said, you maybe looking for something like an IOC / DI framework. See Ninject, StructureMap, Castle Windsor, etc.