-2
我正在使用.dll在C#中進行項目。爲了讓.dll輸出到一個richtextbox(我用作控制檯),我做了一個監聽器來檢查.dll上的一個變量是否發生了變化,如果是,請在richtextbox中輸出該變量。該部分起作用。然而,invoke方法不起作用(我認爲),因爲當我使用invoke時該變量不會改變。這是我的。exe代碼:method.Invoke()似乎沒有工作C#
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();
}
}
}
}
這是我的.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))";
}
}
}
我那麼解決它自己,但我沒有(明顯)。 任何任何想法?提前致謝。
什麼變量不變?我沒有看到任何試圖甚至使用在DLL中調用'guideLine()'的副作用的代碼,但請注意,您的'one()'方法完全改變了'output'變量的值不同的'Class1'實例比你用來調用該方法的實例。您需要訪問該實例才能看到更改,或者完全擺脫'var'字段並僅使用當前實例。我還建議你避免使用「var」作爲變量名,因爲「var」是C#中的保留字。 –
這很難理解你的問題。請提供一個可靠地重現問題的良好[mcve],以及對代碼做什麼以及您希望做什麼的詳細和準確的描述。請注意,出於MCVE的目的,看起來你不需要DLL,因爲它看起來真的有問題,只是處理動態創建的對象。這裏也有很多代碼顯然與這個問題無關。 –