-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathIsolated.cs
51 lines (45 loc) · 1.44 KB
/
Isolated.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
namespace RoliSoft.TVShowTracker
{
using System;
/// <summary>
/// Manages creating and running a type in a separate AppDomain.
/// </summary>
/// <typeparam name="T">The type to be isolated.</typeparam>
public sealed class Isolated<T> : IDisposable where T : MarshalByRefObject
{
private AppDomain _domain;
private T _instance;
/// <summary>
/// Initializes a new instance of the <see cref="Isolated{T}"/> class.
/// </summary>
public Isolated()
{
var type = typeof(T);
_domain = AppDomain.CreateDomain("Isolated:" + type.Name + "/" + Guid.NewGuid(), null, AppDomain.CurrentDomain.SetupInformation);
_instance = (T)_domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
}
/// <summary>
/// Gets the isolated instance.
/// </summary>
/// <value>The instance.</value>
public T Instance
{
get
{
return _instance;
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
if (_domain != null)
{
AppDomain.Unload(_domain);
_domain = null;
_instance = null;
}
}
}
}