method.Invoke()似乎没有工作C#

时间:2016-04-09 18:24:58

标签: c# dll invoke

我正在使用.dll在c#中创建一个项目。要将.dll输出到richtextbox(我用作控制台),我已经建立了一个监听器来检查.dll上的变量是否已更改,如果是,则在richtextbox中输出该变量。那部分有效。但是,调用方法不起作用(我认为),因为当我使用调用时变量没有改变。这是我在.exe中的代码:

gub

这是我的.dll代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;

namespace Dark_Magic_Origins
{
    public partial class game : Form
    {
        static string listenerPath;
        static string prevOutput = String.Empty;
        static Assembly DLL;
        static MethodInfo method;
        static Type theType;
        static object c;

        public game()
        {
            InitializeComponent();
        }

        private void game_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(closingThaForm);

            if (WhatIsNext.whatsNext.Equals("new"))
            {
                timer.Interval = 500;
                timer.Enabled = true;

                loadDLL(Application.StartupPath + @"\bin\story\Chapter 1.dll", "Chapter_1.Class1", "guideLine");

                method.Invoke(c, new object[0]);
            }
        }

        private void closingThaForm(object sender, FormClosingEventArgs e)
        {
            if (SharedVars.closeProgram == true)
            {
                Application.Exit();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtInput.Text))
            {
                System.Media.SystemSounds.Exclamation.Play();
            }
            else if (SharedVars.enableSpeech == true)
            {
                console.Text += Environment.NewLine + "<" + SharedVars.name + "> " + txtInput.Text;
                txtInput.Text = String.Empty;
            }
            else
            {
                System.Media.SystemSounds.Exclamation.Play();
            }
        }

        private void loadDLL(string path, string namespaceDotClass, string Method)
        {
            DLL = Assembly.LoadFile(path);

            theType = DLL.GetType(namespaceDotClass);
            c = Activator.CreateInstance(theType);
            method = theType.GetMethod(Method);
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            IList<FieldInfo> fields = new List<FieldInfo>(theType.GetFields());

            object fValue = new Object();
            foreach (FieldInfo f in fields)
            {
                if (f.Name.Equals("output"))
                {
                    fValue = f.GetValue(c);
                    break;
                }
            }

            if (!prevOutput.Equals(fValue.ToString()))
            {
                console.Text += Environment.NewLine + fValue.ToString();
                prevOutput = fValue.ToString();
            }
        }
    }
}

我试过自己解决这个问题,但是我(显然)失败了。 任何想法?提前谢谢。

1 个答案:

答案 0 :(得分:-1)

抱歉缺乏信息。但是,正如Peter Duniho所暗示的那样,我摆脱了变量变量,这就有效了!谢谢!

为什么我使用.dll?我想以这样的方式制作我的程序,当我想添加新内容时,我只需要创建一个新的.dll。但我首先想要习惯.dll的。这就是为什么这个.dll似乎有点无意义。

感谢您帮助我!

相关问题