转换一个C#控制台应用程序到DLL控制台、应用程序、DLL

由网友(好听的符号,好听带符号的名字)分享简介:我重写从SOAP必发API来JSON和我已经开始了我以前做了它作为一个控制台应用程序的方式,然后叫从任务调度程序或服务取胜。I am rewriting the Betfair API to JSON from SOAP and I have started off the way I did it before a...

我重写从SOAP必发API来JSON和我已经开始了我以前做了它作为一个控制台应用程序的方式,然后叫从任务调度程序或服务取胜。

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.

不过现在我被要求做各种不同的工作与code,我不想写为每个作业一个控制台应用程序(不同的网站想要的价格,下注等)

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)

新的codeBase的比旧的要大得多,我本来可以到4个文件从旧系统复制到一个DLL的应用程序,然后创建不同的控制台应用程序/服务以实现DLL的 - 但是,因为这是40多个文件,我不想复制ñ粘贴的工作,如果可能的。

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.

有没有一种方法,我可以将现有控制台项目很容易地转换成一个类/ DLL项目在VS一些工具或命令?

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();

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

如2/3线code来实现我的注册到我的应用程序的DLL。

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.

我使用VS 2012,.NET 4.5(或4.0,如果我需要根据服务器)时,Windows 8.1

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

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

这个答案是从的此处。同时它使用的WinForms,而不是控制台应用程序,我想你将能够使用它。

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

创建DLL的步骤

第1步: - 文件 - >新建 - >项目 - >的Visual C#项目 - >类库。选择您的项目名称和相应的目录单击确定

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

点击按钮后OK,解决方案资源管理器中增加了一个C#类的Class1.cs。在这个类中,我们可以写我们的code。

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

当我们双击的Class1.cs,我们看到了一个命名空间CreatingDLL。我们将使用这个名称空间在我们的项目中访问此类库。

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.

第二步: - 在我们的Class1.cs创建一个名为总和这有两个整型值的方法,并返回总和巫婆的方法传递的数字。

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;
        }
    }
}

第三步: - 现在构建应用程序,看到斌调试我们的项目目录。 CreatingDLL.dll创建。

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

现在我们创建另一个应用程序,并采取访问DLL的方法,该DLL(CreatingDLL.dll)参考。 用于访问创建DLL步

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

第四步: - 文件 - >新建 - >项目 - >的Visual C#项目 - > Windows窗体应用程序

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

第五步: - 专窗形成为波纹管图

Step 5:- Designed windows form as bellow figure.

第6步: - 添加DLL(CreatingDLL)我们几分钟之前创建的参考

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

在添加DLL的引用,以下窗口会出现。

After adding reference of DLL, following windows will appear.

第7步: - 写$ C $在Windows窗体应用程序按钮点击以下。在创建对象,使添加DLL的方法,在项目为波纹管code添加命名空间CreatedDLL。

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);
            }
        }
    }
}

第8步: - 现在构建应用程序和执行的项目,看看输出

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

编辑:要更改应用程序到库中做这些步骤

首先,双击属性里面的解决方案资源管理器窗口。

First, double click on Properties inside Solution Explorer window.

然后,在openned页面,更改从输出型控制台应用程序为类库

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

阅读全文

相关推荐

最新文章