Skip to content

Instantly share code, notes, and snippets.

@tvolodimir
Created September 18, 2013 14:09
Show Gist options
  • Save tvolodimir/6609668 to your computer and use it in GitHub Desktop.
Save tvolodimir/6609668 to your computer and use it in GitHub Desktop.
CreateProxy Instance in AppDomain
public sealed class Remote<T> where T : MarshalByRefObject
{
private readonly AppDomain domain;
private readonly T remoteObject;
private Remote(AppDomain domain, T remoteObject)
{
this.domain = domain;
this.remoteObject = remoteObject;
}
public T RemoteObject
{
get { return remoteObject; }
}
public AppDomain Domain
{
get { return domain; }
}
public static Remote<T> CreateProxy(AppDomain domain, params object[] constructorArgs)
{
if (domain == null)
{
throw new ArgumentNullException("domain");
}
var type = typeof(T);
var proxy = (T)domain.CreateInstanceAndUnwrap(
type.Assembly.FullName,
type.FullName,
false,
BindingFlags.CreateInstance,
null,
constructorArgs,
null,
null);
return new Remote<T>(domain, proxy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment