[英]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文件中包括名称空间。
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.