# Creating an appdomain

**URL:** https://talk.remobjects.com/t/creating-an-appdomain/6763
**Category:** Snippets
**Tags:** elements, net, oxygene
**Created:** [August 28, 2015, 11:54am UTC](https://talk.remobjects.com/t/creating-an-appdomain/6763 "2015-08-28T11:54:27Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ck](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ck/32/8740_2.png) [@ck](https://talk.remobjects.com/u/ck)
#### Post date: [August 28, 2015, 11:54am UTC](https://talk.remobjects.com/t/creating-an-appdomain/6763/1 "2015-08-28T11:54:27Z")

</div>

A simple example of creating and destroying an AppDomain on .NET, and running code inside the app domain. Generally used to load plugins or other code that should allow for dlls to be unloaded after use.

```auto
  RemoteAppDomain = public class(IDisposable)
  public
    constructor;
    method Dispose;

    property Domain: AppDomain; readonly;
    property RemoteHelper: RemoteHelper; readonly;
  end;

  RemoteHelper = public class(MarshalByRefObject, IDisposable)
  private
  public
    method Run;

    method Dispose;
  end;

implementation

method RemoteHelper.Run;
begin
  writeLn('From app domain!');
end;

method RemoteHelper.Dispose;
begin
  writeLn('Cleanup');
end;

constructor RemoteAppDomain;
begin
  var t := typeOf(RemoteHelper);
  var lDomainSetup := new AppDomainSetup(ApplicationBase := Path.GetDirectoryName(t.Assembly.Location));
  Domain := AppDomain.CreateDomain('RemoteDomain', nil, lDomainSetup);

  RemoteHelper := RemoteHelper(Domain.CreateInstanceAndUnwrap(t.Assembly.FullName, t.FullName, false, BindingFlags.Default, nil, new System.Object[0], nil, nil));
end;

method RemoteAppDomain.Dispose;
begin
  RemoteHelper.Dispose;
  AppDomain.Unload(Domain);
end;

```

This can then be used with:

```auto
  using dom := new RemoteAppDomain do
    dom.RemoteHelper.Run;

```
