分享在winform下实现模块化插件编程
发布日期:2021-05-09 09:35:20 浏览次数:15 分类:博客文章

本文共 16777 字,大约阅读时间需要 55 分钟。

������������������������������������������winform������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������B/S���������������������������������������������������������������������������������������������ERP������������������������������������ERP���������������������������������������������������������������������������������������������������������������������������WEB������������������������������������������������������������������������WINFORM���������������������������������������������������������������������������ERP������������������������������������������������������������������������������������������������������������������������������������������������������������������WINFORM������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������SVN������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������ASP.NET ������������������������������������������

public static class WebApiConfig    {        public static void Register(HttpConfiguration config)        {            config.Routes.MapHttpRoute(                name: "DefaultApi",                routeTemplate: "api/{controller}/{id}",                defaults: new { id = RouteParameter.Optional }            );                   }    }

������������������������������������������config������������������������������������������������������������������������������������������������������������������������

 1.���������������������������������PlugIn������������������������������������������������������������������������������������������������������������������������������������������������������������

IAppContext���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace PlugIn{    ///     /// ���������������������������������    /// ������������������������������������������������������������������������������������������������������������������������������������������������    /// ���������Zuowenjun    /// 2016-3-26    ///     public interface IAppContext    {        ///         /// ������������������        ///         string AppName { get;}        ///         /// ������������������        ///         string AppVersion { get; }        ///         /// ������������������������������������STRING������������������������������������        ///         string SessionUserInfo { get; }        ///         /// ������������������������������������������STRING������������������������������������        ///         string PermissionInfo { get; }        ///         /// ���������������������������������������������������������������������������������������������������        ///         Dictionary
AppCache { get; } ///
/// ��������������������������������������������������������������������������������������� /// Form AppFormContainer { get; } ///
/// ��������������������������������������������������� /// ///
///
Form CreatePlugInForm(Type formType); ///
/// ��������������������������������������������������� /// ///
///
Form CreatePlugInForm(string formTypeName); }}

ICompoent���������������������������������������������������������������������������������������������������������������������������������������������������������������������������

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace PlugIn{    ///     /// ������������������������    /// ���������������������������������������������������������������������������������������������������������������������������������    /// ���������Zuowenjun    /// 2016-3-26    ///     public interface ICompoent    {        ///         /// ������������        ///         string CompoentName { get;}        ///         /// ���������������������������������������        ///         string CompoentVersion { get; }        ///         /// ���������������������������������������������        ///         IEnumerable
FormTypes { get; } }}

ICompoentConfig���������������������������������������������������������������������������������������������������������������������������CompoentRegister������������������������������������������������������

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace PlugIn{    ///     /// ������������������������    /// ���������������������������������������������������������������������������������������������CompoentRegister������������������������������������������������������    /// ���������Zuowenjun    /// 2016-3-26    ///     public interface ICompoentConfig    {        void CompoentRegister(IAppContext context, out ICompoent compoent);    }}

Compoent���������������������������������������������������������������������������ICompoent������������������������������������������������������

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using PlugIn;using System.Windows.Forms;namespace PlugIn{    ///     /// ���������������������    /// ���������Zuowenjun    /// 2016-3-26    ///     public class Compoent : ICompoent    {        private List
formTypeList = new List
(); public string CompoentName { get; private set; } public string CompoentVersion { get; private set; } public IEnumerable
FormTypes { get { return formTypeList.AsEnumerable(); } } public Compoent(string compoentName, string compoentVersion) { this.CompoentName = compoentName; this.CompoentVersion = compoentVersion; } public void AddFormTypes(params Type[] formTypes) { Type targetFormType = typeof(Form); foreach (Type formType in formTypes) { if (targetFormType.IsAssignableFrom(formType) && !formTypeList.Contains(formType)) { formTypeList.Add(formType); } } } }}

2.������������������������������������������������������������������IAppContext���������������AppContext

using PlugIn;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WinFormPlugin{    ///     /// ������������������������������    /// ���������Zuowenjun    /// 2016-3-26    ///     public class AppContext : IAppContext    {        internal static AppContext Current;        internal Dictionary
AppFormTypes { get; set; } public string AppName { get; private set; } public string AppVersion { get; private set; } public string SessionUserInfo { get; private set; } public string PermissionInfo { get; private set; } public Dictionary
AppCache { get; private set; } public System.Windows.Forms.Form AppFormContainer { get; private set; } public AppContext(string appName, string appVersion, string sessionUserInfo, string permissionInfo, Form appFormContainer) { this.AppName = appName; this.AppVersion = appVersion; this.SessionUserInfo = sessionUserInfo; this.PermissionInfo = permissionInfo; this.AppCache = new Dictionary
(); this.AppFormContainer = appFormContainer; } public System.Windows.Forms.Form CreatePlugInForm(Type formType) { if (this.AppFormTypes.ContainsValue(formType)) { return Activator.CreateInstance(formType) as Form; } else { throw new ArgumentOutOfRangeException(string.Format("���������������{0}������������������������������������������������������������", formType.FullName), "formType"); } } public System.Windows.Forms.Form CreatePlugInForm(string formTypeName) { Type type = Type.GetType(formTypeName); return CreatePlugInForm(type); } }}

���������AppContext���������������������������������������������AppContext���������������������������������������������������������Load���������������������

private void ParentForm_Load(object sender, EventArgs e)        {            AppContext.Current = new AppContext("������������������������", "V16.3.26.1", "admin", "administrator", this);            AppContext.Current.AppCache["loginDatetime"] = DateTime.Now;            AppContext.Current.AppCache["baseDir"] = AppDomain.CurrentDomain.BaseDirectory;            AppContext.Current.AppFormTypes = new Dictionary
(); LoadComponents(); LoadMenuNodes(); } private void LoadComponents() { string path = AppContext.Current.AppCache["baseDir"] + "com\\"; Type targetFormType = typeof(Form); foreach (string filePath in Directory.GetFiles(path, "*.dll")) { var asy = Assembly.LoadFile(filePath); var configType = asy.GetTypes().FirstOrDefault(t => t.GetInterface("ICompoentConfig") != null); if (configType != null) { ICompoent compoent=null; var config = (ICompoentConfig)Activator.CreateInstance(configType); config.CompoentRegister(AppContext.Current,out compoent);//������������������������������������������������compoent if (compoent != null) { foreach (Type formType in compoent.FormTypes)//������������������������������������AppContext���AppFormTypes��� { if (targetFormType.IsAssignableFrom(formType)) { AppContext.Current.AppFormTypes.Add(formType.FullName, formType); } } } } } } private void LoadMenuNodes() //������������������������������������������������������������������������������ { this.treeView1.Nodes.Clear(); var root = this.treeView1.Nodes.Add("Root"); foreach (var formType in AppContext.Current.AppFormTypes) { var node = new TreeNode(formType.Key) { Tag = formType.Value }; root.Nodes.Add(node); } }

������������������������������������������������������������

private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)        {            if (e.Node.Nodes.Count <= 0)//������������������������������������������������            {                ShowChildForm(e.Node.Tag as Type);            }        }        private void ShowChildForm(Type formType)        {            var childForm= Application.OpenForms.Cast
().SingleOrDefault(f=>f.GetType()==formType); if (childForm == null) { childForm = AppContext.Current.CreatePlugInForm(formType); //(Form)Activator.CreateInstance(formType); childForm.MdiParent = this; childForm.Name = "ChildForm - " + DateTime.Now.Millisecond.ToString(); childForm.Text = childForm.Name; childForm.Show(); } else { childForm.BringToFront(); childForm.Activate(); } }

3.������������������������������������������������������������������������WINDOWS������������������������������������������������������������������������������������������������������������FORM������������������Com.First���������������������������������������������PlugIn���������������ICompoentConfig���������������CompoentConfig

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using PlugIn;namespace Com.First{    ///     /// ���������������������������������������������������������������ICompoentConfig���    /// ���������Zuowenjun    /// 2016-3-26    ///     public class CompoentConfig : ICompoentConfig    {        public static IAppContext AppContext;        public void CompoentRegister(IAppContext context,out ICompoent compoent)        {            AppContext = context;            var compoentInfo = new Compoent("Com.First", "V16.3.26.1.1");            compoentInfo.AddFormTypes(typeof(Form1), typeof(Form2));//���������������������������������������������������������������            compoent = compoentInfo;//������Compoent���������        }    }}

���������������������������������������������������������������������������������������������������������DLL���Com.First.Dll���������������������������������������com������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������Com.First������������������������Form1������������������Form1���������������������������������������������������������������������������

private void Form1_Load(object sender, EventArgs e)        {            CompoentConfig.AppContext.AppFormContainer.FormClosing += AppFormContainer_FormClosing;        }        void AppFormContainer_FormClosing(object sender, FormClosingEventArgs e)        {            MessageBox.Show(label1.Text + ",���������������������������������������������������");            e.Cancel = true;        }        private void Form1_FormClosed(object sender, FormClosedEventArgs e)        {            CompoentConfig.AppContext.AppFormContainer.FormClosing -= AppFormContainer_FormClosing;        }

���������������������

 

������������������������������������Form2���������������������������������������������������������������������������������������������������������������

private void button1_Click(object sender, EventArgs e)        {            if (CompoentConfig.AppContext.PermissionInfo.Equals("user",StringComparison.OrdinalIgnoreCase))            {                MessageBox.Show(this.Name);            }            else            {                MessageBox.Show("������������" + CompoentConfig.AppContext.SessionUserInfo + "���������������������" + CompoentConfig.AppContext.PermissionInfo + "���������������������user���������������������");            }        }

���������������������

���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������

上一篇:分享在winform下实现模块化插件编程-优化版
下一篇:由面试引发的思考:B/S与C/S究竟是何物

发表评论

最新留言

感谢大佬
[***.8.128.20]2025年04月29日 09时05分56秒