简体   繁体   English

从命令行运行简单的C#程序时,如何使用我创建的DLL?

[英]How can I use a DLL I created when running a simple C# program from the command line?

I need to know how to use the command line to compile and run C# from the command line but I cannot figure out how to correctly use a DLL I made. 我需要知道如何使用命令行从命令行编译和运行C#,但是我无法弄清楚如何正确使用我制作的DLL。 The DLL is a simple maths file the code inside is: DLL是一个简单的数学文件,其内部代码为:

using System;

namespace SimpleMaths
{
public class Operations
{

    public static int add(int a, int b)
    {
        return a + b;
    }

    public static int substract(int a, int b)
    {
        return a - b;
    }

    public static int multiply(int a, int b)
    {
        return a * b;
    }
}
}

I successfully compiled it as a DLL file. 我已成功将其编译为DLL文件。 Now I have a class called sum.cs, and in there I want to access the methods of this library, I did have a lot of messy code in there which was nowhere near right so I deleted it, here is what I have now: 现在,我有一个名为sum.cs的类,并且我想在其中访问该库的方法,我那里确实有很多凌乱的代码,而且这些代码附近都不在,所以我删除了它,这就是我现在所拥有的:

using System;
public class sum
{
public static void Main()
{

//How can I access the methods in the SimpleMaths library???
}
}

Usually in Visual Studio I would add the DLL as a reference and then create objects which would give me access to the methods. 通常在Visual Studio中,我会添加DLL作为参考,然后创建使我可以访问方法的对象。 I have searched on-line and the examples I have found are confusing me. 我已经在线搜索了,发现的例子使我感到困惑。 Am I right in saying that you cannot create objects using the command line? 我说对了,您不能使用命令行创建对象吗?

Any advice is appreciated. 任何建议表示赞赏。

You need to add a reference to the DLL in VS, then put this at the top of your application. 您需要在VS中添加对DLL的引用,然后将其放在应用程序的顶部。

using LibraryNamespace

Then you can call the functions in the DLL from your sum class using 然后,您可以使用sum类从DLL中调用函数

YourLibrary.LibraryClass.LibraryMethod

You need to use /reference in the command line. 您需要在命令行中使用/ reference。

csc /reference:library1.dll /reference:library2.dll csfile.cs

Also make sure to include the namespace in your .cs file. 还要确保在.cs文件中包括名称空间。

The MS command line tool is Powershell. MS命令行工具是Powershell。
This is a good example how to use a .NET DLL interactively from Powershell. 是一个很好的示例,说明如何从Powershell交互使用.NET DLL。

You cannot run a DLL directly from the command line. 您不能直接从命令行运行DLL。 You need to host it in some kind of executable file (.exe). 您需要将其托管在某种可执行文件(.exe)中。

You need to add another project of type command line application. 您需要添加另一个类型为命令行应用程序的项目。 This will contain your main method. 这将包含您的主要方法。 Youi should then reference your DLL from there. 然后,Youi应该从那里引用您的DLL。

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

相关问题 如何使用Python脚本中的C#DLL? - How can I use a C# DLL from Python Script? 如何解决“找不到元数据foo.dll!” 命令行上编译C#代码时出错? - How can I resolve 'Metadata foo.dll not found!' errors when compiling C# code on the command line? 如何在C#程序中使用SOS.dll进行自动调试? - How can I use SOS.dll within a C# program for automated debugging purposes? 在 C# DLL 文件中作为进程运行时,如何获取控制台应用程序输出? - How can I obtain console application output when running it as a process in a C# DLL file? 当我使用命令行构建C#/ .NET时,如何设置配置? - How do I setup configuration when I use command line to build C#/.NET? C#从命令行运行表单程序时隐藏表单 - C# Hiding form when running form program from the command line 从Windows 7命令行运行时,为什么我的C#程序会经常暂停? - Why does my C# program pause so often when running from the Windows 7 command line? 我可以从命令行重命名C#符号吗? - Can I rename a C# symbol from the command line? 当失去焦点时如何使C#命令行程序保持运行 - How to keep the C# command line program still running when it loses focus 从 C# 运行 bash 命令时出现无法执行二进制文件错误 - I get a can't execute binary file error when running a bash command from C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM