C# 方法。Invoke()在C中似乎不起作用

C# 方法。Invoke()在C中似乎不起作用,c#,dll,invoke,C#,Dll,Invoke,我正在用c语言用.dll做一个项目。为了让.dll输出到我用作控制台的richtextbox,我制作了一个侦听器来检查.dll上的变量是否已更改,如果已更改,则在richtextbox中输出该变量。那部分有效。然而,我认为invoke方法不起作用,因为当我使用invoke时变量没有改变。这是我在.exe中的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da

我正在用c语言用.dll做一个项目。为了让.dll输出到我用作控制台的richtextbox,我制作了一个侦听器来检查.dll上的变量是否已更改,如果已更改,则在richtextbox中输出该变量。那部分有效。然而,我认为invoke方法不起作用,因为当我使用invoke时变量没有改变。这是我在.exe中的代码:

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();
            }
        }
    }
}
我试着自己解决,但显然失败了。
有人有什么想法吗?提前谢谢。

很抱歉没有信息。但是我按照Peter Duniho的建议去掉了var变量,这很有效!谢谢

为什么我要使用.dll?我想让我的程序在这样一种方式,当我想添加新的内容,我只需要做一个新的.dll。但我首先想习惯.dll。这就是为什么这个.dll看起来有点毫无意义


谢谢你帮助我

什么变量没有改变?我没有看到任何代码试图在DLL中使用调用guideLine的副作用,但请注意,one方法会在与调用该方法完全不同的Class1实例中更改输出变量的值。您需要访问该实例以查看更改,或者完全去掉var字段,只使用当前实例。我还建议您避免使用var作为变量名,因为var在C中是一个保留字。您的问题很难理解。请提供一个可靠地再现问题的好代码,并详细准确地描述该代码的功能以及您希望它执行的功能。请注意,就MCVE而言,您似乎不需要DLL,因为看起来您真正遇到的问题只是处理动态创建的对象。这里还有很多代码显然与问题无关。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter_1
{
    public class Class1
    {
        public string output = String.Empty;
        static Class1 var = new Class1();

        public void guideLine()
        {
            one();
        }

        private void one()
        {
            var.output = "Once upon a time, in the realm of Sehrli..." +
                Environment.NewLine + "...there was a... a... what, actually? From which race are you?" +
                Environment.NewLine + "1) Human" +
                Environment.NewLine + "2) Elf" +
                Environment.NewLine + "3) Dwarf" +
                Environment.NewLine + "4) Guardian" +
                Environment.NewLine + "5) Angel" +
                Environment.NewLine + "6) Qanadli" +
                Environment.NewLine + "(Type the number of the option you want to select, or type 'info' and a number to get more info (e.g. info 3))";
        }
    }
}