应用域创建对象

应用域创建对象
一般步骤:
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();
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值