它是一个不好的做法来引用C#项目中的exe文件 [英] Is it a bad practice to reference an exe file in C# project

查看:121
本文介绍了它是一个不好的做法来引用C#项目中的exe文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。

我知道我可以在我的C#项目引用的.NET可执行文件。

I know that I can reference an .net executable file in my C# project.

我不想做与输出类型:Windows应用程序不必要的项目,只是调用某些DLL

I don't want to make unnecessary project with "Output Type: Windows Application" just to call some dlls.

我只是想知道的是它OK或者是一个不好的做法,refrence一个exe文件?

I just want to know is it OK or is it a bad practice to refrence an exe file?

推荐答案

是的,这可以被看作是一种不好的做法为原因如下:

Yes, this can be seen as a bad practice for the following reasons:


  • 坏工程建筑结果
    。如果你需要调用从一个.exe一些逻辑,那么该逻辑被错误地放在那里。相反,你应该把它放在一个单独的DLL和来自两个可执行相同的.dll您目前参考参考,并引用可执行应用程序。
    的建议中的下面意见,提取逻辑到库可以帮助您避免一些CPU架构的限制,我将在我的下一个点形容,因为本库可以建立针对任何CPU。

  • Bad Project Architecture
    If you need to call some logic from an .exe, then that logic is incorrectly placed there. Instead, you should put it in a separate dll and reference that same .dll from both the executable you reference currently, and the application that references the executable. As suggested in comments below, extracting the logic into a library can help you avoid some CPU architecture limitations, which I will describe in my next point, as the library can be built to target any CPU.

建筑限制结果
中的引用的可执行文件可能已建成最佳解决32位或64位机器上,或甚至特定的CPU(如安腾)。库可以在没有这些规范建设 1 ,以便跨CPU兼容,因此可以通过任何一个项目以后引用。如果引用具体的架构设置的可执行文件,你应该使用兼容的设置来引用项目。我考虑的限制。

Architecture Limitations
The referenced executable might have been built to address optimally 32 bit or 64 bit machines, or even specific CPUs (like Itanium). A library can be built without these specifications1 in order to be cross-CPU-compatible, and thus be referenced by any project later. If you reference an executable with specific architecture settings, you should use compatible settings to the referencing project. That I consider a limitation.

潜在的授权问题结果
你应该仔细分发应用程序。如果引用的可执行文件需要以额外的许可证或收费使用,则必须强制用户接受一起应用程序的一个可执行执照(如有需要付费),否则你作出非法传播风险它与您的软件。这也意味着,您必须引用可执行文件摆在首位权

未知的后果结果
中的可执行文件将在bin文件夹中复制并一起安装应用程序。有没有说服力,如果有人浏览bin文件夹并执行它会发生什么。有与几个问题:

Unknown Consequences
The executable will be copied within the bin folder and installed alongside your application. There is no telling what could happen if someone browses the bin folder and executes it. There are a few problems with that:


  • 可执行崩溃,或因行为不端输入不正确的。通常这种情况下,如果没有任何GUI它(例如,如果一个命令行程序双击用户也不会在命令行参数的形式得到任何输入,从而会崩溃,或者表现不好)。

  • The executable crashes, or misbehaves because of improper input. Usually this happens if it does not have any GUI (for instance if a command-line program is double-clicked by user it will not get any input in the form of command-line arguments and thus crash, or misbehave).

可执行无意通过你的程序的所有者使用,因为这将在法律上或逻辑上自相矛盾到你的软件做什么。

The executable is not intended to be used by the owner of your program, as that would legally or logically contradict to what your software does.

然而,也有一些情况下,引用一个可执行文件是合理的,但这些都是罕见的足够的:

Yet, there are some cases where referencing an executable can be justified, but those are rare enough:


  • 可执行来自的第三方,然后用相同的功能没有图书馆的存在,有没有其他方法链接到该功能。它也可能是您的雇主或客户建立的项目明​​确的要求。

  • 的可执行文件是的写在另一种语言并需要通过互操作与它通信

  • The executable comes from a 3rd party, and no library with the same functionality exists, and there is no other way to link to that functionality. It also might be explicit requirement for your project established by your employer or client.
  • The executable is written in another language and you need to communicate with it via interop.

只要后者不适用于你,特别是如果你开发所引用自己的可执行文件,我肯定会建议抽取所需的逻辑独立的库。

As long as the latter do not apply to you, and especially if you develop the executable that is referenced yourself, I would definitely recommend to extract the needed logic to a separate library.

1 其实你也可以建立一个可执行针对任何CPU,通过的多米尼克Kexel的评论。相反的情况也是可能的 - 以建立特定的CPU的库,但它是不常见,作为可执行通常一个被定制的硬件。
所以,澄清我的观点,我脑子里想引用第三方的可执行文件,或者一个不能重建其他原因,而可执行已经为一些特定的架构进行了优化。如果你的可以重建和可执行变化目标的CPU,那么你绝对可以抽取所需的逻辑放到一个dll。

1 In fact you can also build an executable to target any CPU, as mentioned by Dominic Kexel's comment. The opposite is also possible - to build a library for specific CPU, but it is less common, as the executable is usually the one being tailored to the hardware. So, to clarify my points, I had in mind referencing a 3rd party executable, or one that cannot be rebuilt for other reasons, and that executable is already optimized for some specific architecture. If you can rebuild and change that executables' targeted CPU, then you can definitely extract the needed logic into a dll.

这篇关于它是一个不好的做法来引用C#项目中的exe文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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