Created
December 8, 2016 12:25
-
-
Save Powerz/38c54004636e081c800918312a00c8e7 to your computer and use it in GitHub Desktop.
Run a method in another application domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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