zoukankan      html  css  js  c++  java
  • 插件的简单原理

    using IExtendServices;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace 插件的简单原理
    {
        public class ExtendServices
        {
            private MenuStrip ms = null;
            private RichTextBox txt = null;
            public ExtendServices(MenuStrip ms, RichTextBox txt)
            {
                this.ms = ms;
                this.txt = txt;
            }
            public void Service()
            {
                string[] plugFilesPath = Directory.GetFiles(Path.Combine(Application.StartupPath, "plugs"), "*.dll");
    
                foreach (string itemPath in plugFilesPath)
                {
                    Assembly ass = Assembly.LoadFrom(itemPath);
                    //获取所有的公共类型
                    Type[] assTypes = ass.GetExportedTypes();
                    //自定义的接口类型
                    Type itype = typeof(IExtendService);
                    foreach (Type itemtype in assTypes)
                    {
                        if (itemtype.IsPublic && itype.IsAssignableFrom(itemtype) && !itemtype.IsAbstract&&!itemtype.IsInterface)
                        {
                            IExtendService service = (IExtendService)Activator.CreateInstance(itemtype);
                            ToolStripMenuItem editItem = (ToolStripMenuItem)ms.Items[1];
    
                            ToolStripItem seritem = editItem.DropDownItems.Add(service.Name);
                            seritem.Tag = service;
                            seritem.Click += SerItemMethod;
                        }
                    }
    
                }
            }
    
            private void SerItemMethod(object sender, EventArgs e)
            {
                ToolStripItem service = (ToolStripItem)sender;
                ((IExtendService)service.Tag).Service(txt);
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CNotePadPlugs
    {
        public class ToASCII:IExtendServices.IExtendService
        {
            public string Name
            {
                get { return "ToASCII"; }
            }
    
            public void Service(System.Windows.Forms.RichTextBox txt)
            {
    
                string all = txt.Text;
                string txtSelected = txt.SelectedText;
    
                if (txtSelected == "")
                    txtSelected = all;
                
                int index = txt.SelectionStart;//2
                if (index == all.Length)
                    index = 0;
                //0
                string start = all.Substring(0, index);//2
                //1
                string upper = all.Substring(index, txtSelected.Length);
                char[] chars = upper.ToCharArray();
                upper = "";
                foreach (char item in chars)
                {
                    upper += (int)item;
                }
                //2
                string end = txt.Text.Substring(index + txtSelected.Length);
    
                txt.Text = start + upper + end;    
    
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace IExtendServices
    {
        public interface IExtendService
        {
            string Name
            {
                get;
            }
            void Service(System.Windows.Forms.RichTextBox txt);
        }
    }

    源文件:http://pan.baidu.com/s/1ntp0XxZ

  • 相关阅读:
    Java Web工作原理
    Java——入门“HelloWorld”
    Java——介绍
    vscode给 vue 项目 添加 eslint 验证提示遇到的细节问题
    使用全局变量,当多个线程同时修改静态属性第一季
    自编验证码图片识别程序 自定义的FloodFill函数
    nginx卸载ssl django明文
    nginx 7层全转发
    JAVA Socket入门详解(基本用法与代码实现)
    小白进入公司究竟有哪些不同?学生一定要看!
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/3778518.html
Copyright © 2011-2022 走看看