简体   繁体   English

Visual Studio 2008项目组织,用于可执行文件和程序集

[英]Visual Studio 2008 project organization for executable and assembly

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. 我在Visual Studio 2008中进行以下设置时遇到问题:包含入口点Main()方法类并声明接口的父项目,以及具有实现在父项目中声明的接口的类的子项目。

I have specified that Parent's Output type is a Console application, and Child's Output type is a Class library. 我已指定Parent的Output类型为Console应用程序,Child的Output类型为Class库。 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. 构建成功,据我所知,正确的内容显示在Child / bin / debug目录中:Parent.exe和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: 但是,如果我运行Parent.exe,则应该从Child.dll加载类时,它会失败并显示错误消息:

exception executing operation System.TypeLoadException: Could not load type 'Child.some.class' from assembly 'Parent, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. 执行操作System.TypeLoadException的异常:无法从程序集“父母”,版本= 1.0.0.0,区域性=中性,“公共密钥令牌=空”中加载类型“ Child.some.class”。

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. 我计划有更多的子项目使用与Parent中设置的框架相同的框架,因此我不想将入口点类下移到Child项目中。 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). 如果我尝试指定子项目也是控制台应用程序,则构建过程将失败,因为子项中没有Main()入口点类(即使将父项目包括在内)也是如此。

Any help would be welcome! 任何帮助都将受到欢迎! Thanks, Martin 谢谢,马丁

Generally you want to reverse your hierarchy: your .exe project should depend on your satellite assembly projects (what you're calling "Child"). 通常,您想颠倒等级:您的.exe项目应取决于您的附属程序集项目(您称其为“子级”)。 Then, to use the types in your satellite assembly, you want to add a reference from your .exe project to your assembly project. 然后,要使用附属程序集中的类型,您想将.exe项目中的引用添加到程序集项目中。 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. 通过在VS的解决方案资源管理器中右键单击.exe项目来执行此操作,然后执行“添加引用...”,单击“项目”选项卡并选择组装项目。

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. 将这些定义放在MyProject.Core.dll之类的文件中。 Then your "implementing" projects / assemblies would have a reference to your MyProject.Core project. 然后,您的“实施”项目/程序集将具有对MyProject.Core项目的引用。 With all this said, you maybe looking for something like an IOC / DI framework. 综上所述,您可能正在寻找类似IOC / DI框架的内容。 See Ninject , StructureMap , Castle Windsor , etc. 参见NinjectStructureMapCastle Windsor等。

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. 如果希望将接口和实现分开,请在“子”程序集(类库)使用的第三个程序集(类库)中定义接口,然后在应用程序中使用接口和实现。

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. 因此,按照回答自己的问题的优良传统,我得出结论,为了成功调用GetType() ,必须指定正确的程序集。 Each project is compiled into its own assembly ( Application.exe , Interface.dll , and Implementation.dll ). 每个项目都被编译成自己的程序集( Application.exeInterface.dllImplementation.dll )。 So in order to instantiate the implementation class within the Application Main() class, I have to call: 因此,为了实例化Application Main()类中的实现类,我必须调用:

Type actionType = Type.GetType("ImplementationClass", "Implementation", true);

It seems a little hokey, but at least it works. 似乎有些曲折,但至少可以奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM