creates three instances of the same type. A local and two remote proxies to object instances are constructed:
using System;
using System.Reflection;
using System.Runtime.Remoting;
class MyClass {
public void MethodA(DateTime dt) {
Console.WriteLine("MethodA invoked at " + dt.ToLongTimeString());
}
}
class Starter {
static void Main() {
CreateLocal();
CreateRemote1();
CreateRemote2();
}
static void CreateLocal() {
object obj = Activator.CreateInstance(typeof(MyClass));
((MyClass)obj).MethodA(DateTime.Now);
}
static void CreateRemote1() {
ObjectHandle hObj = Activator.CreateInstance("library","MyClass");
object obj = hObj.Unwrap();
MethodInfo method = obj.GetType().GetMethod("MethodA");
method.Invoke(obj, new object[1] { DateTime.Now });
}
static void CreateRemote2() {
AppDomain domain = AppDomain.CurrentDomain;
object obj = domain.CreateInstanceFromAndUnwrap("library.dll","MyClass");
MethodInfo method = obj.GetType().GetMethod("MethodA");
method.Invoke(obj, new object[1] { DateTime.Now });
}
}
Related examples in the same category