Skip to content

Instantly share code, notes, and snippets.

@Powerz
Created December 8, 2016 12:25
Show Gist options
  • Save Powerz/38c54004636e081c800918312a00c8e7 to your computer and use it in GitHub Desktop.
Save Powerz/38c54004636e081c800918312a00c8e7 to your computer and use it in GitHub Desktop.
Run a method in another application domain
public class IsolatedDomainScope<T> : IDisposable where T : new()
{
private AppDomain _domain;
public IsolatedDomainScope()
{
_domain = AppDomain.CreateDomain("IsolatedDomainScope:" + Guid.NewGuid(), null, AppDomain.CurrentDomain.SetupInformation);
var type = typeof(T);
Value = (T) _domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
}
public T Value { get; }
public void Dispose()
{
if (_domain == null) return;
AppDomain.Unload(_domain);
_domain = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment