1

我正在尝试制作一个简单的文本编辑器,可以实时为文本着色。我还必须为此使用 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
            }
        }
    }
}
4

1 回答 1

2

是的,您的代码使用反射。这些行是一个示例:

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

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

于 2013-01-10T18:32:51.810 回答