Executing Code in Another Application Domain (C# Programming Guide)

Visual Studio 2005

 

Once an assembly has been loaded into an application domain, the code it contains can be executed. The simplest way to do this is to use AssemblyLoad which will load the assembly into the current application domain, and begin running the code at the assembly's default entry point.

If you want to load the assembly into another application domain, use ExecuteAssembly or ExecuteAssemblyByName, or one of the other overloaded versions of these methods.

If you want to execute the other assembly starting other than at the default entry point, define a new type in the remote assembly, deriving from MarshalByRefObject. Then use CreateInstance to create an instance of that type in your application.

Consider the following file, which creates an assembly consisting of a single namespace and two classes. Assume this assembly has been built, and is stored on drive C with the name HelloWorldRemote.exe.

C#

// This namespace contains code to be called.
namespace HelloWorldRemote
{
    public class RemoteObject : System.MarshalByRefObject
    {
        public RemoteObject()
        {
            System.Console.WriteLine("Hello, World! (RemoteObject Constructor)");
        }
    }
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Hello, World! (Main method)");
        }
    }
}

To access the code from another application, you can either load the assembly into the current application domain or create a new application domain and load the assembly into it. If you load the assembly into the current application domain with Assembly.LoadFrom, you can use Assembly.CreateInstance to instantiate an instance of the RemoteObject class, which causes the object constructor to be executed.

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

C#

static void Main()
{
    // Load the assembly into the current appdomain:
    System.Reflection.Assembly newAssembly = System.Reflection.Assembly.LoadFrom(@"c:\HelloWorldRemote.exe");

    // Instantiate RemoteObject:
    newAssembly.CreateInstance("HelloWorldRemote.RemoteObject");
}

When loading the assembly into a separate application domain, use AppDomain.ExecuteAssembly to access the default entry point or AppDomain.CreateInstance to create an instance of the RemoteObject class. Creating the instance causes the constructor to be executed.

C#

static void Main()
{
    System.AppDomain NewAppDomain = System.AppDomain.CreateDomain("NewApplicationDomain");

    // Load the assembly and call the default entry point:
    NewAppDomain.ExecuteAssembly(@"c:\HelloWorldRemote.exe");

    // Create an instance of RemoteObject:
    NewAppDomain.CreateInstanceFrom(@"c:\HelloWorldRemote.exe", "HelloWorldRemote.RemoteObject");
}

If you do not want to load the assembly programmatically, use Add Reference from the Solution Explorer to specify the assembly HelloWorldRemote.exe. Then, add a using HelloWorldRemote; directive to the using block of your application, and use the RemoteObject type in your program to declare an instance of the RemoteObject object, like this:

C#

static void Main()
{
    // This code creates an instance of RemoteObject, assuming HelloWorldRemote has been added as a reference:
    HelloWorldRemote.RemoteObject o = new HelloWorldRemote.RemoteObject();
}

 

[출처] https://msdn.microsoft.com/en-us/library/ms173139(v=vs.80).aspx

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
24 윈도우즈 마켓 앱 스토어 개발 안내서  가을의곰 2017.06.18 154
23 Assembly.GetType 메서드 file 가을의곰 2017.06.18 140
» Executing Code in Another Application Domain (C# Programming Guide) 가을의곰 2017.06.18 142
21 1. 리플렉션(Reflection) 가을의곰 2017.06.18 159
20 .NET Reflection  가을의곰 2017.06.18 176
19 C# - Interfaces 가을의곰 2017.06.18 151
18 C# 인터페이스  가을의곰 2017.06.18 262
17 Public Key Tokens with sn 졸리운_곰 2017.05.27 188
16 [C#] 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. [출처] C# : 오류 System.NullReferenceException: 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.(오류 System.NullReferenceException) 졸리운_곰 2017.05.27 2354
15 private, public, protected 그리고 internal?? 접근 한정자에 대해 알아보자. 졸리운_곰 2017.05.27 163
14 [.NET Framework] .NET : 368. 닷넷의 어셈블리 서명 데이터 확인 방법 [링크 복사], [링크+제목 복사] file 졸리운_곰 2017.05.27 331
13 [Tips] Visual Studio 에서 특정 어셈블리의 PublicKeyToken 찾는 법 졸리운_곰 2017.05.27 409
12 .net의 publickeytoken 찾는 sn 프로그램 경로 졸리운_곰 2017.05.27 213
11 예외 문제 해결: System.BadImageFormatException 졸리운_곰 2017.05.27 932
10 A replacement for MemoryStream file 졸리운_곰 2017.05.20 197
9 [.net] jni4net 을 이용하여 c# .net 에서 .jar 파일 사용하기 file 졸리운_곰 2017.04.26 384
8 A Simple Crawler Using C# Sockets file 졸리운_곰 2017.04.24 273
7 ILSpy 닷넷 어셈블리 디컴파일러 오픈 소스 file 졸리운_곰 2017.04.22 428
6 C# WinForm "인증서 저장소에서 매니페스트 서명 인증서를 찾을 수 없습니다." file 졸리운_곰 2017.03.18 741
5 Getting started with SQLite in C# file 졸리운_곰 2017.03.18 286
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED