C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

程序域(一个程序中调用另一个程序)

一个exe中可以调用另一个exe,或另一个exe的方法:
-------------用程序域实现:
AppDomain 类用于创建和中断应用程序域。

1建立控制台AssemblyA

using System;

namespace Wrox.ProCSharp.Assemblies.AppDomains
{
 
class Class1
 
{
  
public Class1(int val1, int val2)
  
{
   Console.WriteLine(
"Constructor with the values {0}, {1}" +
    
" in domain {2} called", val1, val2,
    AppDomain.CurrentDomain.FriendlyName);
  }

  [STAThread]
  
static void Main(string[] args)
  
{
   Console.WriteLine(
"Main in domain {0} called",
    AppDomain.CurrentDomain.FriendlyName);
  }

 }

}

产生。exe。
2建立控制台DomainTest
using System;
namespace Wrox.ProCSharp.Assemblies.AppDomains
{
    
class Test
    
{
        [STAThread]
        
static void Main(string[] args)
        
{
            AppDomain currentDomain 
= AppDomain.CurrentDomain;
            Console.WriteLine(currentDomain.FriendlyName);
            AppDomain secondDomain 
= 
                AppDomain.CreateDomain(
"New AppDomain");
        
//    secondDomain.ExecuteAssembly("AssemblyA.exe");
            secondDomain.CreateInstance("AssemblyA"
                
"Wrox.ProCSharp.Assemblies.AppDomains.Class1"true,
                System.Reflection.BindingFlags.CreateInstance,
                
nullnew object[] {73}nullnullnull);

        }

    }

}

结果:
DomainTest.exe
Constructor with the values 7, 3 in domain New AppDomain called
Press any key to continue

DomainTest.exe
Constructor with the values 7, 3 in domain New AppDomain called
Press any key to continue


---------------------用程序集的反射也可以实现一个exe调用另一个exe的方法!
:system.Type 可以访问任何给定的数据类型的信息
:system.Assembly访问给定程序集的元数据,也可以加载和执行程序集的方法

posted on 2006-01-05 16:38 梦在天涯 阅读(1310) 评论(1)  编辑 收藏 引用 所属分类: C#/.NET

评论

# re: 程序域(一个程序中调用另一个程序) 2006-04-19 10:55 梦在天涯

上面第一个的exe必须放在第二个的bin\下


secondDomain.ExecuteAssembly("AssemblyA.exe");调用其main()方法.
  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1785158
  • 排名 - 5

最新评论

阅读排行榜