反射——1 通过反射写记事本插件

知识:反射,接口,程序集
写插件的主要步骤:
1 在主程序的执行目录下新建一个文件夹plugins
2 在主程序中遍历plugins中满足记事本接口的文件
3 找到以后对使用dll对文本文件进行操作
(主要通过1 反射获取dll文件的路径,以及元数据; 2 定义接口类型的变量调用接口中定义的方法)

 

完整项目代码

1 定义记事本接口

namespace Iplugins
{
    public interface IAddPlugins
    {
        string Name
        {
            get;
        }

        void startProgram(TextBox txtbox);
    }
}
View Code

2 记事本主程序

namespace _01Note
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //1 检查plugins目录下是否存在.dll文件
            //获取当前执行程序的目录
            string startPath = Assembly.GetExecutingAssembly().Location;
            string pluginsPath = Path.Combine(Path.GetDirectoryName(startPath), "plugins");//使用combine可以不用考虑“\”
            //将所有dll文件路径保存在一个String数组中
            string[] dlls = Directory.GetFiles(pluginsPath, "*.dll");

            //2 plugins目录下是否存在.dll文件存在
            //2.1加载plugins目录下的程序集
            foreach (string item in dlls)
            {
                Assembly asem = Assembly.LoadFile(item);
                //获取所有的public类型
                Type[] dllsType = asem.GetExportedTypes();
                //获取接口类型
                Type IpluginsType = typeof(IAddPlugins);

                //循环遍历遍历每一个dll文件中的类型是否实现了IPlugins接口
                foreach (Type dllsTypeItem in dllsType)
                {
                    if (IpluginsType.IsAssignableFrom(dllsTypeItem) && !dllsTypeItem.IsAbstract)
                    {
                        IAddPlugins addPlugins = (IAddPlugins)Activator.CreateInstance(dllsTypeItem);
                        ToolStripItem tspItem = this.格式ToolStripMenuItem.DropDownItems.Add(addPlugins.Name);
                        //由于addPlugins是局部变量,可以通过tag属性,设置包含有关项的数据的对象。
                        tspItem.Tag = addPlugins;
                        //给菜单栏按钮添加事件
                        tspItem.Click += new EventHandler(tsp_click);
                    }
                }

            }
        }

        private void tsp_click(object sender, EventArgs e)
        {
            ToolStripItem tsp = (ToolStripItem)sender;
            IAddPlugins addplugins = (IAddPlugins)tsp.Tag;
            addplugins.startProgram(textBox1);
        }
    }
}
View Code

3 改变成大写插件

namespace Plugins_changeUpper
{
    public class changUpper : IAddPlugins
    {
        public string Name
        {
            get
            {
                return "大写";
            }
        }

        public void startProgram(System.Windows.Forms.TextBox txtbox)
        {
            txtbox.Text = txtbox.Text.ToUpper();
        }
    }
}
View Code

4 改变样式插件

//类库代码

 public class ChangeStyle : IAddPlugins
    {
        public string Name
        {
            get
            {
                return "改变样式";
            }
        }

        public void startProgram(TextBox txtbox)
        {
            Form1 styleForm = new Form1(txtbox);
            styleForm.Show();
        }
    }
//窗体代码
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private TextBox _txtBox;
        public Form1(TextBox txtBox):this()
        {
            _txtBox = txtBox;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float fontSize = float.Parse(comboBox1.Text);
            _txtBox.Font = new Font(comboBox2.Text, fontSize);
            this.Close();
        }
    }
View Code

5 添加时间插件

namespace Plugins_AddTime
{
    public class AddTime : IAddPlugins
    {
        public string Name
        {
            get
            {
                return "添加时间";
            }
        }

        public void startProgram(System.Windows.Forms.TextBox txtbox)
        {
            txtbox.Text += "\r\n";
            txtbox.Text += "\t\t\t\t"+DateTime.Now.ToString();
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/lv-sally/p/4727131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace NoteBook { /// <summary> /// About 的摘要说明。 /// </summary> public class About : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public About() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(About)); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.ForeColor = System.Drawing.Color.Red; this.label1.Image = ((System.Drawing.Image)(resources.GetObject("label1.Image"))); this.label1.Location = new System.Drawing.Point(0, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(496, 128); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // label2 // this.label2.Font = new System.Drawing.Font("华文新魏", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label2.Location = new System.Drawing.Point(16, 147); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(192, 32); this.label2.TabIndex = 1; this.label2.Text = "记事本编辑器"; // // label3 // this.label3.Font = new System.Drawing.Font("华文行楷", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label3.ForeColor = System.Drawing.Color.RoyalBlue; this.label3.Location = new System.Drawing.Point(16, 224); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(232, 24); this.label3.TabIndex = 2; this.label3.Text = " 版权所有,翻版必究!"; // // label4 // this.label4.Font = new System.Drawing.Font("华文新魏", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134))); this.label4.Location = new System.Drawing.Point(240, 151); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(248, 32); this.label4.TabIndex = 3; this.label4.Text = "计算机3063班 C#课程设计 "; // // label5 // this.label5.Image = ((System.Drawing.Image)(resources.GetObject("label5.Image"))); this.label5.Location = new System.Drawing.Point(256, 197); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(240, 64); this.label5.TabIndex = 4; // // About // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.Black; this.ClientSize = new System.Drawing.Size(496, 262); this.Controls.Add(this.label5); this.Controls.Add(this.label4); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.ForeColor = System.Drawing.Color.Red; this.Name = "About"; this.Text = "关于我的记事本"; this.ResumeLayout(false); } #endregion } }
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值