是否可以将相同的DLL放入控制台应用程序和NuGet依赖项中? [英] Is it possible to make the same DLL into both a console application and a NuGet dependency?

查看:86
本文介绍了是否可以将相同的DLL放入控制台应用程序和NuGet依赖项中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对.NET Standard 1.5的项目,该项目作为NuGet上的多个DLL部署。该项目是从Java移植的。在项目的某些类内是静态的 Main()方法,这些方法应从命令行运行。

I have a project that targets .NET Standard 1.5 that is deployed as several DLLs on NuGet. The project was ported from Java. Inside some of the classes of the project are static Main() methods that are meant to be run from the command line.

在.NET Core中,似乎有 2种编译DLL的方法

In .NET Core it seems there are 2 ways to compile a DLL:


  • < OutputType> Exe< / OutputType> ; -编译为可执行控制台应用程序(DLL)

  • < OutputType> Library< / OutputType> (默认)-编译成类库(DLL)

  • <OutputType>Exe</OutputType> - compiles into an executable console app (DLL)
  • <OutputType>Library</OutputType> (default) - compiles into a class library (DLL)

我想知道是否有一种方法可以编译DLL,因此可以使用这两种方式而没有2个单独的(令人困惑的)DLL?

What I am wondering is there a way to compile the DLL so it can be used either way without having 2 separate (confusing) DLLs?

基本上,我试图获得与Java类似的功能,其中程序包可以由在命令行上运行的应用程序引用(并指定尝试在命令行上定位目标)。

Basically, I am trying to get similar functionality to that in Java where a package can be referenced by an application or run on the command line (and to specify the entry target on the command line).

例如,在Java中,包都包含的文件包含静态 Main(object [] args)方法。

For example, in Java there are files that are both part of the package and contain a static Main(object[] args) method.

public class SomeClass
{
    public void DoSomething(string arg1, string arg2, string arg3)
    {
        // implementation...
    }

    public static void Main(object[] args)
    {
         // parse args...

         new SomeClass().DoSomething(arg1, arg2, arg3);
    }
}

DoSomething在包中的其他地方引用(等效到我的NuGet包现在的样子)。但是,在Java中, Main(object [] args)可以从命令行运行,例如...

DoSomething is referenced elsewhere within the package (which is equivalent to what my NuGet packages look like now). However, in Java the Main(object[] args) can be run from the command line like...

java <package>.jar <namespace>.SomeClass [args]

,而无需下载或安装其他任何东西。毕竟,如果用户要在其上运行命令的组件在那里,所有的依赖项也都在那里。

without having to download or install anything extra. After all, if the component the user wants to run the command on is there, all of the dependencies are there, too.

理想情况下,我可以使用 dotnet核心中的类似功能 ...

Ideally, I can just use similar functionality in dotnet core...

dotnet <assembly>.dll <namespace>.SomeClass [args]

最好是围绕整个对象创建一个单独的包装DLL,或者创建一个必须选择并选择 SomeClass 所有依赖项的单独项目,以便对它们进行编译

which would be preferable to having to create a separate wrapper DLL around the whole thing, or make a separate project that has to pick and choose all of the dependencies of SomeClass so they are all compiled into a single assembly (console app).

此外,每个包有几种静态 Main()方法,

Furthermore, there are several static Main() methods per package, which seems to have been supported in .NET Core earlier which is what my other question is about.

推荐答案

使用.Net Standard / Core,最好的选择是第二个p roject编译为一个EXE,该EXE具有一个指向库版本的简单Main()方法。这并不理想,但问题是.Net core的运行时如何工作。面向.Net Standard的项目可以被与此标准版本兼容的所有项目使用。面向特定.NetCoreApp的项目只能由其他.NetCoreApps引用,因此,您无法获得针对标准的好处。

With .Net Standard/Core, your best option is to have a second project that compiles to an EXE who has a simple Main() method that points to the library's version. It isn't ideal, but the issue is how the runtime for .Net core works. A project that targets .Net Standard can be used by all projects compatible with that standard version. A project that targets a specific .NetCoreApp can only be referenced by other .NetCoreApps, so you don't get the benefit of targeting the standard.

对于打包/ NuGet部署,可以将控制台应用程序版本放入NuGet的tools文件夹中,以便最终用户可以使用它。您真的不希望通过标准的NuGet内容文件夹来部署可执行文件,因为它不会真正遵循标准,并且会使您的NuGet用户感到困惑。

For the packaging/NuGet deployment, the console app version can be put into the tools folder of the NuGet so an end user can use it. You don't really want executables to be deployed via the standard NuGet content folder because it won't really follow the standard and be confusing for your NuGet users.

这篇关于是否可以将相同的DLL放入控制台应用程序和NuGet依赖项中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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