C#:控制台程序调用中间库创建窗体

1、类库项目引用System.Windows.Forms并添加引用后,才可创建窗体。

2、控制台应用程序调用中间库(DLL)中的方法创建窗体;中间类库使用反射下的Assembly加载包含窗体的类库及创建实例。

注意:1)创建实例时,参数为窗体类的全名(命名空间+类名)。

     2)返回值是Object类型,需转化为Form类型。

         3)exe(控制台程序)、中间类库(dll)、窗体所在类库(dll)在同一目录下。

         4)Load(空间名,窗体类全名),LoadFrom(*.dll,窗体类全名)

具体代码如下:

窗体类库:

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;

namespace TestForm
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }
    }
}

中间类库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Anxin.Factory
{
    public class FormFactory
    {
        /// <summary>
        /// 创建窗体
        /// </summary>
        /// <param name="val_classFullName"></param>
        /// <returns></returns>
        public static Form CreateForm(string val_classFullName)
        {
            string nameSpace = val_classFullName.Substring(0,val_classFullName.LastIndexOf('.'));
            return (Form)Assembly.Load(nameSpace).CreateInstance(val_classFullName);
        }

        /// <summary>
        /// 创建窗体
        /// </summary>
        /// <param name="val_nameSpace">命名空间</param>
        /// <param name="val_className">窗体类名</param>
        /// <returns></returns>
        public static Form CreateForm(string val_nameSpace, string val_className)
        {
            //string className = val_className;  //仅使用类名创建示例失败。
            string classFullName = string.Format("{0}.{1}",val_nameSpace,val_className);    
            //MessageBox.Show(Assembly.GetExecutingAssembly().Location);
            return (Form)Assembly.Load(val_nameSpace).CreateInstance(classFullName);
        }

        public static Form CreateForm(string val_formAssemblyFile, string val_formFullName, Object[] val_formArgs)
        {
            return CreateForm(val_formAssemblyFile, val_formFullName, val_formArgs, null);
        }


        public static Form CreateForm(string val_formAssemblyFile,string val_formFullName,Object[] val_formArgs,string val_formText)
        {
            Form form;
            string assemblyPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string assemblyFullName = assemblyPath + val_formAssemblyFile;
            if (!assemblyPath.EndsWith("\"))
            {
                assemblyFullName = assemblyPath + "\" + val_formAssemblyFile;
            }
            //MessageBox.Show(assemblyFullName);
            Assembly formAssembly = Assembly.LoadFrom(assemblyFullName);
            form = formAssembly.CreateInstance(val_formFullName) as Form;
            if(null == form)
            {
                string strError = string.Format("创建窗体失败!
程序集{0}
窗体类名称{1}",
                        val_formAssemblyFile,
                        val_formFullName);
                if (!string.IsNullOrEmpty(val_formText))
                {
                    strError += "窗体标题" + val_formText;
                }
                throw new Exception(strError);
            }

            if (!string.IsNullOrEmpty(val_formText))
            {
                form.Text = val_formText;
            }

            return form;
        }
    }
}

控制台程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Anxin.Factory;
using System.Windows.Forms;

namespace TestFactory
{
    class Program
    {
        static void Main(string[] args)
        {
            string nameSpace = "TestForm";
            string className = "Test";   //窗体类名
            //string className = "Example";  //窗体文件名 (报错)
            Form testForm = FormFactory.CreateForm(nameSpace,className);

            string classFullName = nameSpace + "." + className;
            testForm = FormFactory.CreateForm(classFullName);

            string dllName = "TestForm.dll";
            //string classFullName = "TestForm.Test";
            testForm = FormFactory.CreateForm(dllName,classFullName,null);

            string formText = "OK 加油!";
            testForm = FormFactory.CreateForm(dllName, classFullName, null,formText);

            if (null != testForm)
            {
                testForm.ShowDialog();
            }
            else
            {
                Console.WriteLine("通过调用Dll,创建窗体失败!
");
                //使用窗体文件名创建时失败!
            }
        }
    }
}
原文地址:https://www.cnblogs.com/shenchao/p/4409765.html