应用域创建对象
一般步骤:
1、创建应用域,若需要应用域属性,则可通过SetData来设置。
AppDomain child = AppDomain.CreateDomain("childDomain");
child.SetData("PRIVATE_BINPATH","mybin");
2、在应用域中创建对象。
ObjectHandle oh = (ObjectHandle) child.CreateInstance("mydll","Demo.MyRefObj");
3、获得实际对象。
Demo.MyRefObj refobj = (Demo.MyRefObj)oh.Unwrap();
4、对象的一些操作。
refobj.DoSomeWork();
实例:
1、实现mydll.dll
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace Demo
{
public class MyObj
{
public void DoSomeWork()
{
Console.WriteLine("我是Demo.MyObj对象");
Console.WriteLine("当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程的代码是:{0}",Thread.CurrentThread.GetHashCode().ToString());
}
}
public class MyRefObj : MarshalByRefObject
{
public void DoSomeWork()
{
Console.WriteLine("我是Demo.MyRefObj对象");
Console.WriteLine("当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程的代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
public MyRefObj()
{
Console.WriteLine("MyRefObj构造函数:当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程的代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
}
[Serializable]
public class MyValueObj
{
public int i=0;
public int j=0;
public void DoSomeWork()
{
Console.WriteLine("我是Demo.MyValueObj");
Console.WriteLine("当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
public MyValueObj()
{
Console.WriteLine("MyValueObj构造函数:当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
}
}
2、创建控制台应用
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Threading;
using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace Demo
{
public class MyObj
{
public void DoSomeWork()
{
Console.WriteLine("我是Demo.MyObj对象");
Console.WriteLine("当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程的代码是:{0}",Thread.CurrentThread.GetHashCode().ToString());
}
}
public class MyRefObj : MarshalByRefObject
{
public void DoSomeWork()
{
Console.WriteLine("我是Demo.MyRefObj对象");
Console.WriteLine("当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程的代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
public MyRefObj()
{
Console.WriteLine("MyRefObj构造函数:当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程的代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
}
[Serializable]
public class MyValueObj
{
public int i=0;
public int j=0;
public void DoSomeWork()
{
Console.WriteLine("我是Demo.MyValueObj");
Console.WriteLine("当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
public MyValueObj()
{
Console.WriteLine("MyValueObj构造函数:当前应用域的名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程代码为:{0}/n",Thread.CurrentThread.GetHashCode().ToString());
}
}
}
2、创建控制台应用
using System;
using System.Reflection;
using System.Runtime.Remoting;
using System.Threading;
namespace AppDemo
{
public class App
{
static private void ShowInfo()
{
Console.WriteLine("当前应用域名字为:{0}",AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("当前线程代码为:{0}",Thread.CurrentThread.GetHashCode().ToString());
}
static private void demoCreateObject()
{
App.ShowInfo();
AppDomain child = AppDomain.CreateDomain("ChildDomain");
child.SetData("PRIVATE_BINPATH","mybin");
//Console.WriteLine("/n演示在程序集中创建非引用或传值对象");
ObjectHandle oh = (ObjectHandle)child.CreateInstance("mydll","Demo.MyObj");
//Demo.MyObj obj= (Demo.MyObj)oh.Unwrap();
Console.WriteLine("/n演示在程序集中创建引用对象");
oh = (ObjectHandle)child.CreateInstance("mydll","Demo.MyRefObj");
Demo.MyRefObj refobj = (Demo.MyRefObj)oh.Unwrap();
refobj.DoSomeWork();
Console.WriteLine("/n演示在程序集中创建传值对象");
oh = (ObjectHandle) child.CreateInstance("mydll","Demo.MyValueObj");
Demo.MyValueObj valobj= (Demo.MyValueObj)oh.Unwrap();
valobj.DoSomeWork();
AppDomain.Unload(child);
}
static public void Main()
{
Console.WriteLine("开始demoCreateObject:/n");
demoCreateObject();
}
}
}