Can .NET AppDomains do this?

I read about AppDomains for many hours, but I'm not sure if they work the way I hope.

If I have two classes, common Foo <T> in AppDomain # 1, Bar in AppDomain # 2:

Appendix No. 1 is an application. App Domain # 2 is something like a plugin, and it can be loaded and unloaded dynamically.

AppDomain # 2 wants to create a Foo <Bar> and use it. Foo <T> uses many classes in AppDomain # 1 internally.

I don't want AppDomain # 2 to use a foo object with reflection, I want it to use Foo <Bar> foo, with all the static typing and compiled speed that comes with it. Can this be done, given that AppDomain # 1 containing Foo <T> is never unloaded?

If so, is there any deletion when using Foo <Bar>?

When I unload AppDomain # 2, is the Foo <Bar> type destroyed?

to edit . SO removed all my> added back manually.

+3
source share
2 answers

You mix types and objects in your question, which makes the answer difficult. The code in AD does not cause problems using types that are also used in other ADs; it just loads the assembly. But AD has its own garbage heap; you cannot directly reference objects that live in another AD. They must be serialized along the AD boundary. Yes, Remoting through IpcChannel will do this.

+6
source

-, MarshalByRefObject; - . , ( , ).

, , , - :

using System;
using System.Reflection;

namespace TestAppDomain
{
class Program
{
    static void Main(string[] args)
    {
        AppDomain pluginsAppDomain = AppDomain.CreateDomain("Plugins");

        Foo foo = new Foo();
        pluginsAppDomain.SetData("Foo", foo);

        Bar bar= (Bar) pluginsAppDomain.CreateInstanceAndUnwrap(Assembly.GetEntryAssembly().FullName, typeof (Bar).FullName);
        bar.UseIt();

        AppDomain.Unload(pluginsAppDomain);
        foo.SaySomething();
    }
}

class Bar : MarshalByRefObject
{
    public void UseIt()
    {
        Console.WriteLine("Current AppDomain: {0}", AppDomain.CurrentDomain.FriendlyName);
        Foo foo = (Foo) AppDomain.CurrentDomain.GetData("Foo");
        foo.SaySomething();
    }
}

class Foo : MarshalByRefObject
{
    public void SaySomething()
    {
        Console.WriteLine("Something from AppDomain: {0}", AppDomain.CurrentDomain.FriendlyName);
    }
}
}

( ), , , ( ) , .

, .

+1

Source: https://habr.com/ru/post/1747874/


All Articles