使用反射为文本着色

本文关键字:文本 反射 | 更新日期: 2023-09-27 17:51:19

我正在尝试制作一个简单的文本编辑器,实时为文本着色。我也必须使用DLL和反射。我想在用户打字时给文本上色。因此,我有一个复选框。如果选中,文本将在用户输入时被着色(实时)。

我已经写了一个DLL文件来做到这一点。

不管怎么说,我对反射是很陌生的。

  • :

我想问一下你们的专业建议,我写的东西是否可以被称为"使用反射"?如果不是,能告诉我哪里不对吗?

这是我的代码(我已经从它删除了很多东西,所以代码将反映问题,但可能有剩菜)

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;
namespace Editor
{
    public class MainForm : Form
    {
        //Declaration of Controls 
        private RichTextBox EditRichTextBox;
        private CheckBox chkBox;
        private int flag = 0;
        private Button[] PlugButton;
        public string[] PluginNames;
        private int NumofPlugins;
        public MainForm()
        {
            //Initialization of Controls
            this.EditRichTextBox = new RichTextBox();
            this.ErrorTextBox = new RichTextBox();
        this.chkBox = new CheckBox();
            //Form
            this.ClientSize = new Size(700, 500);
            this.Name = "MainForm";
            this.Text = "C# Editor";
            //EditRichTextBox
            this.EditRichTextBox.Location = new Point(20, 20);
            this.EditRichTextBox.Name = "EditRichTextBox";
            this.EditRichTextBox.Size = new Size(this.Width - 150, 300);
            this.EditRichTextBox.AcceptsTab = true;
            this.EditRichTextBox.Multiline = true;
            //Controls on the Form
        this.Controls.Add(this.ButtonCompilelib);
            this.Controls.Add(this.ButtonCompile);
            this.Controls.Add(this.ButtonRun);
            this.Controls.Add(this.EditRichTextBox);
            this.Controls.Add(this.ErrorTextBox);
        this.Controls.Add(this.chkBox);
        //CheckBox
        this.chkBox.Location = new Point(600,300);
            this.chkBox.Name = "chkBox";
            this.chkBox.Text = "Color";
        };
        //My checkbox handler
        this.chkBox.Click += (sender,e) =>
        {
            if(flag == 0)
            {
                flag = 1;
                MessageBox.Show("Coloring Text");
                }
            else 
                flag = 0;
        };
        //My TextBox handler
        this.EditRichTextBox.KeyPress += (sender,e) =>
        {
            try
            {
                string tmp = Environment.CurrentDirectory + "''" + "mydll" + ".dll";            Assembly a = Assembly.LoadFrom(tmp);
                Type t = a.GetType("MyPlugIn.colorclass");
                MethodInfo mi = t.GetMethod("color");
                Object obj = Activator.CreateInstance(t);
                Object[] Params = new Object[5];
                Params[0] = EditRichTextBox.Text;
                Params[1] = EditRichTextBox.Handle;
                Params[2] = ErrorTextBox.Handle;
                Params[3] = EditRichTextBox;
                Params[4] = flag;
                mi.Invoke(obj, Params);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        };

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
}

这是DLL文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace MyPlugIn
{
    public class colorclass
    {
        public void color(string Text, Object Hndl_Text, Object Hndl_Err, RichTextBox box,int flag)
        {
            if (flag == 1)
            {
                int start = box.TextLength;
                int end = box.TextLength;
                //Textbox may transform chars, so (end-start) != text.Length
                box.Select(start, end - start);
                {
                    box.SelectionColor = Color.Blue;
                }
                box.SelectionLength = 0; // clear
            }
        }
    }
}

使用反射为文本着色

是的,你的代码使用了反射。以下几行是一个示例:

Type t = a.GetType("MyPlugIn.colorclass");
MethodInfo mi = t.GetMethod("color");
Object obj = Activator.CreateInstance(t);

这是否是最好的方法,或者对于这项任务是否必要,这是另一个话题。