2016-04-09 1 views
-2

Je crée un projet en C# en utilisant un fichier .dll. Pour laisser la sortie .dll à une riche zone de texte (que j'utilise en tant que console) j'ai fait un écouteur pour vérifier si une variable sur le fichier .dll a changé, et si c'est le cas, afficher cette variable dans la zone riche. Cette partie fonctionne. Cependant, la méthode invoke ne fonctionne pas (je pense) parce que la variable ne change pas quand j'utilise l'invoke. Ceci est mon code dans le fichier .exe:method.Invoke() ne semble pas fonctionner 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(); 
      } 
     } 
    } 
} 

Ceci est mon code .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))"; 
     } 
    } 
} 

J'ai essayé moi-même si résous, mais je Failed (évidemment). Quelqu'un a des idées? Merci d'avance.

+0

Quelle variable ne change pas? Je ne vois pas de code qui tente même d'utiliser l'effet secondaire d'appeler 'guideLine()' dans la DLL, mais notez que votre méthode 'one()' change la valeur de la variable 'output' dans un complètement instance différente de 'Class1' que vous utilisez pour appeler la méthode. Vous devez accéder à cette instance pour voir la modification, ou vous débarrasser complètement du champ 'var' et utiliser simplement l'instance actuelle. Je vous recommande également d'éviter d'utiliser "var" comme nom de variable, car "var" est un mot réservé en C#. –

+0

Il est difficile de comprendre votre question. S'il vous plaît fournir un bon [mcve] qui reproduit fidèlement le problème, avec une description détaillée et précise de ce que fait ce code et ce que vous voulez faire à la place. S'il vous plaît noter que pour les besoins d'un MCVE, il semble que vous n'avez pas besoin de la DLL, car il semble que vous avez vraiment des problèmes avec l'objet créé dynamiquement. Il y a aussi beaucoup de code ici qui n'a clairement rien à voir avec la question. –

Répondre

-1

Désolé pour le manque d'information. Mais je me suis débarrassé de la variable var comme Peter Duniho l'a suggéré, et cela a fonctionné! Donc merci!

Pourquoi est-ce que j'utilise un .dll? Je veux faire mon programme de telle sorte que quand je veux ajouter un nouveau contenu, j'ai juste besoin de faire un nouveau fichier .dll. Mais je voulais d'abord m'habituer à .dll. C'est pourquoi ce .dll semble un peu inutile.

Merci de m'avoir aidé!