Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   [gelöst] Liste "by Reference" zwischen AppDomains übergeben (https://www.delphipraxis.net/65542-%5Bgeloest%5D-liste-reference-zwischen-appdomains-uebergeben.html)

Waldteufel 17. Mär 2006 18:04


[gelöst] Liste "by Reference" zwischen AppDomains
 
Hi Community. :hi:

Dieses Problem macht mich schon seit Tagen verrückt. In meinem Programm verwende ich mehrere AppDomains. Nun möchte ich in der einen AppDomain ein Objekt (genauer gesagt: eine Liste) aus einer anderen AppDomain verändern.

Beispiel:
Code:
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Beispiel {
  class dasObjekt : MarshalByRefObject {
    public Dictionary<string, object> config;

    public dasObjekt() { config = new Dictionary<string, object>(); }
    public void huhu() {
      AppDomain myDomain = AppDomain.CreateDomain("myDomain");
      myType myObject = (myType) myDomain.CreateInstanceAndUnwrap(
                          Assembly.GetAssembly(typeof(myType)).FullName,
                          typeof(myType).FullName
                        );

      myObject.dingsda(this);
    }
 
    public static void Main() {
      dasObjekt x = new dasObjekt();
      x.config["x"] = "y";
      x.huhu();

      foreach (KeyValuePair<string, object> y in x.config)
        Console.WriteLine("config[\"{0}\"] = \"{1}\"", y.Key, y.Value);
      Console.ReadLine();
    }
  }

  class myType : MarshalByRefObject {
    public myType() {
    }
 
    public void dingsda(dasObjekt x) {
      x.config["a"] = "b";
    }
  }
}
Dummerweise kommt diese Änderung (config["a"] = "b") aber nie bei dasObjekt an.
Das Programm gibt nur
Code:
config["x"] = "y"
anstatt von
Code:
config["x"] = "y"
config["a"] = "b"
aus. :-(

Weiß jemand, wie man dieses Problem beseitigen könnte?

[edit 2006-03-18 14:18] Formulierung etwas geradegebogen...

Waldteufel 18. Mär 2006 16:47

Re: [C#] Liste "by Reference" zwischen AppDomains
 
Hi Community. :hi:

:bounce1:


Ich hab das Problem selbst gelöst. Man muss einfach nur eine Methode definieren, die die Setzung des Werts in der Liste übernimmt, und schon klappts. :wall:

Code:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Remoting;
 
namespace xyxyxyx {
  class dasObjekt : MarshalByRefObject {
    public Dictionary<string, object> config;
 
    public dasObjekt() {
      config = new Dictionary<string, object>();
    }
 
    public void setConf(string key, object val) {
      config[key] = val;
    }
 
    public void huhu() {
      AppDomain myDomain = AppDomain.CreateDomain("myDomain");
 
      myType myObject = myDomain.CreateInstanceAndUnwrap(
                          Assembly.GetAssembly(typeof(myType)).FullName,
                          typeof(myType).FullName
                        ) as myType;
 
      myObject.dingsda(this);
    }
 
    public void dump() {
      foreach (KeyValuePair<string, object> y in config)
        Console.WriteLine("config[{0}] = {1}", y.Key, y.Value);
    }
 
    public static void Main() {
      dasObjekt x = new dasObjekt();
      x.huhu();
      x.dump();
      Console.ReadLine();
    }
  }
 
  class myType : MarshalByRefObject {
    public myType() {
    }
 
    public void dingsda(dasObjekt x) {
      x.setConf("x", "y");
    }
  }
}
Wenn man sich vor Augen hält, welcher Code in welcher AppDomain ausgeführt wird, erscheint es im Nachhinein ziemlich simpel.


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:53 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz