Converting a C# Console App to a DLL

2019-02-22 07:21发布

I am rewriting the Betfair API to JSON from SOAP and I have started off the way I did it before as a console APP which is then called from a task scheduler or win service.

However now I have been asked to do various different jobs with the code and I don't want to write a console app for each job (different sites want prices, bets placed etc)

The new codebase is much larger than the old one and I would have been able to copy the 4 files from the old system into a DLL app and then create various console apps/services to implement the DLL - however because it's 40+ files I don't want a copy n paste job if possible.

Is there a way I can EASILY convert an existing console project into a class / DLL project with some tool or command in VS?

I want to be able to just then create simple apps that just go

BetfairBOT myBOT = new BetfairBOT()
myBOT.RunGetPrices();

or

BetfairBOT myBOT = new BetfairBOT()
myBOT.RunPlaceBets();

e.g 2/3 lines of code to implement my DLL that is registered to my app.

So without copy and paste can I do this.

I am using VS 2012, .NET 4.5 (or 4.0 if I need to depending on server), Windows 8.1

Any help would be much appreciated.

1条回答
仙女界的扛把子
2楼-- · 2019-02-22 08:00

This answer is from here. while it used winforms instead of console application, I think you will be able to use it.

Steps for creating DLL

Step 1:- File->New->Project->Visual C# Projects->Class Library. Select your project name and appropriate directory click OK

Creating C# Class Library (DLL) Using Visual Studio .NET

After Clicking on button ‘OK’, solution explorer adds one C# class ‘Class1.cs’. In this class we can write our code.

Creating C# Class Library (DLL) Using Visual Studio .NET

When we double click on Class1.cs, we see a namespace CreatingDLL. We will be use this namespace in our project to access this class library.

Creating C# Class Library (DLL) Using Visual Studio .NET

Step 2:- Within Class1.cs we create a method named ‘sum’ that takes two integers value and return sum to witch method passed numbers.

using System;

namespace CreatingDLL
{
    public class Class1
    {
        /// <summary>
        /// sum is method that take two integer value and return that sum
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int sum(int x, int y)
        {
            return x + y;
        }
    }
}

Step 3:- Now build the Application and see bin\debug directory of our project. ‘CreatingDLL.dll’ is created.

Now we create another application and take this DLL (CreatingDLL.dll) reference for accessing DLL’s method. Steps for accessing created DLL

Step 4:- File->New->Project->Visual C# Projects->Windows Form Application.

Step 5:- Designed windows form as bellow figure.

Creating C# Class Library (DLL) Using Visual Studio .NET

Step 6:- Add reference of DLL (CreatingDLL) which we created before few minutes.

Creating C# Class Library (DLL) Using Visual Studio .NET

Creating C# Class Library (DLL) Using Visual Studio .NET

After adding reference of DLL, following windows will appear.

Creating C# Class Library (DLL) Using Visual Studio .NET

Step 7:- Write code on button click of Windows Form Application. Before creating object and making method of Add DLL, add namespace CreatedDLL in project as bellow code.

using System;
using System.Windows.Forms;

using CreatingDLL;

namespace AccessingDLL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Class1 c1 = new Class1();
            try
            {
                txtResult.Text = Convert.ToString(c1.sum(Convert.ToInt32(txtNumber1.Text), Convert.ToInt32(txtNumber2.Text)));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Step 8:- Now build the application and execute project and see output.

Creating C# Class Library (DLL) Using Visual Studio .NET

Edit: To change an application into a library do these steps

First, double click on Properties inside Solution Explorer window.

Double Click

Then, On the openned page, change the Output Type from Console Application to Class Library

Change output type

查看更多
登录 后发表回答