C# 窗体应用改成类库输出

类库你就新建类库文件直接生成dll去引用好了,新建一个windows窗体把他搞成dll去引用,

先新建第一个窗体应用,

代码如下;


using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetCode2());

        }
        public static string GetCode2()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("using System;");
            sb.Append(Environment.NewLine);
            sb.Append("namespace DynamicCodeGenerate");
            sb.Append(Environment.NewLine);
            sb.Append("{");
            sb.Append(Environment.NewLine);
            sb.Append("    public class HelloWorld");
            sb.Append(Environment.NewLine);
            sb.Append("    {");
            sb.Append(Environment.NewLine);
            sb.Append("        public string OutPut()");
            sb.Append(Environment.NewLine);
            sb.Append("        {");
            sb.Append(Environment.NewLine);
            sb.Append("             return \"Hello world!\";");
            sb.Append(Environment.NewLine);
            sb.Append("        }");
            sb.Append(Environment.NewLine);
            sb.Append("    }");
            sb.Append(Environment.NewLine);
            sb.Append("}");

            string code = sb.ToString();

            return code;
        }

    }
}

点击按钮运行后结果如下:

C# 窗体应用改成类库输出_第1张图片

 

然后我把它的输出类型改为类库:C# 窗体应用改成类库输出_第2张图片

 

然后去新建第二个窗体应用,然后引用上面的项目

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp12;

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

        private void button1_Click(object sender, EventArgs e)
        {
            //WindowsFormsApp12.Form1 f1 = new WindowsFormsApp12.Form1();
            MessageBox.Show( WindowsFormsApp12.Form1.GetCode2());
           
        }
    }
}

执行后:

C# 窗体应用改成类库输出_第3张图片

 

你可能感兴趣的:(C#,技术大杂烩,c#,开发语言)