简体   繁体   中英

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. The DLL is a simple maths file the code inside is:

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. 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:

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. 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.

using LibraryNamespace

Then you can call the functions in the DLL from your sum class using

YourLibrary.LibraryClass.LibraryMethod

You need to use /reference in the command line.

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

Also make sure to include the namespace in your .cs file.

The MS command line tool is Powershell.
This is a good example how to use a .NET DLL interactively from Powershell.

You cannot run a DLL directly from the command line. You need to host it in some kind of executable file (.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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