Tải bản đầy đủ - 0 (trang)
3-8. Pass Data Between Application Domains

3-8. Pass Data Between Application Domains

Tải bản đầy đủ - 0trang

CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



semantics, or a System.Runtime.Serialization.SerializationException will be thrown. (See recipe 3-3

for details on the characteristics required to allow objects to transcend application domain boundaries.)

When using the SetData or GetData methods to exchange data between application domains, you

should avoid using the following keys, which are already used by the .NET Framework:





APP_CONFIG_FILE







APP_NAME







APPBASE







APP_CONFIG_BLOB







BINPATH_PROBE_ONLY







CACHE_BASE







CODE_DOWNLOAD_DISABLED







DEV_PATH







DYNAMIC_BASE







DISALLOW_APP







DISALLOW_APP_REDIRECTS







DISALLOW_APP_BASE_PROBING







FORCE_CACHE_INSTALL







LICENSE_FILE







PRIVATE_BINPATH







SHADOW_COPY_DIRS



The Code

The following example demonstrates how to use the SetData and GetData methods by passing a

System.Collections.ArrayList between two application domains. After passing a list of pets to a second

application domain for modification, the application displays both the original and modified lists.

Notice that the code running in the second application domain does not modify the original list because

ArrayList is a pass-by-value type, meaning that the second application domain only has a copy of the

original list. (See recipe 3-2 for more details.)

using System;

using System.Reflection;

using System.Collections;

namespace Apress.VisualCSharpRecipes.Chapter03

{

public class ListModifier

{



122



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



public ListModifier()

{

// Get the list from the data cache.

ArrayList list = (ArrayList)AppDomain.CurrentDomain.GetData("Pets");

// Modify the list.

list.Add("turtle");

}

}

class Recipe03_08

{

public static void Main()

{

// Create a new application domain.

AppDomain domain = AppDomain.CreateDomain("Test");

// Create an ArrayList and populate with information.

ArrayList list = new ArrayList();

list.Add("dog");

list.Add("cat");

list.Add("fish");

// Place the list in the data cache of the new application domain.

domain.SetData("Pets", list);

// Instantiate a ListModifier in the new application domain.

domain.CreateInstance("Recipe03-08",

"Apress.VisualCSharpRecipes.Chapter03.ListModifier");

// Display the contents of the original list.

Console.WriteLine("Original list contents:");

foreach (string s in list)

{

Console.WriteLine(" - " + s);

}

// Get the list and display its contents.

Console.WriteLine("\nModified list contents:");

foreach (string s in (ArrayList)domain.GetData("Pets"))

{

Console.WriteLine(" - " + s);

}

// Wait to continue.

Console.WriteLine("\nMain method complete. Press Enter.");

Console.ReadLine();

}

}

}



123



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



3-9. Unload Assemblies and Application Domains

Problem

You need to unload assemblies or application domains at runtime.



Solution

You have no way to unload individual assemblies from a System.AppDomain. You can unload an entire

application domain using the static AppDomain.Unload method, which has the effect of unloading all

assemblies loaded into the application domain.



How It Works

The only way to unload an assembly is to unload the application domain in which the assembly is

loaded. Unfortunately, unloading an application domain will unload all the assemblies that have been

loaded into it. This might seem like a heavy-handed and inflexible approach, but with appropriate

planning of your application domain, the assembly loading structure, and the runtime dependency of

your code on that application domain, it is not overly restrictive.

You unload an application domain using the static AppDomain.Unload method and passing it an

AppDomain reference to the application domain you want to unload. You cannot unload the default

application domain created by the CLR at startup.

The Unload method stops any new threads from entering the specified application domain and calls

the Thread.Abort method on all threads currently active in the application domain. If the thread calling

the Unload method is currently running in the specified application domain (making it the target of a

Thread.Abort call), a new thread starts in order to carry out the unload operation. If a problem is

encountered unloading an application domain, the thread performing the unload operation throws a

System.CannotUnloadAppDomainException.

While an application domain is unloading, the CLR calls the finalization method of all objects in the

application domain. Depending on the number of objects and nature of their finalization methods, this

can take an variable amount of time. The AppDomain.IsFinalizingForUnload method returns true if the

application domain is unloading and the CLR has started to finalize contained objects; otherwise, it

returns false.



The Code

This code fragment demonstrates the syntax of the Unload method:

// Create a new application domain.

AppDomain newDomain = AppDomain.CreateDomain("New Domain");

// Load assemblies into the application domain.

...

// Unload the new application domain.

AppDomain.Unload(newDomain);



124



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



3-10. Retrieve Type Information

Problem

You need to obtain a System.Type object that represents a specific type.



Solution

Use one of the following:





The typeof operator







The static GetType method of the System.Type class







The GetType method of an existing instance of the type







The GetNestedType or GetNestedTypes method of the Type class







The GetType or GetTypes method of the Assembly class







The GetType, GetTypes, or FindTypes method of the System.Reflection.Module

class



How It Works

The Type class provides a starting point for working with types using reflection. A Type object allows you

to inspect the metadata of the type, obtain details of the type’s members, and create instances of the

type. Because of its importance, the .NET Framework provides a variety of mechanisms for obtaining

references to Type objects.

One method of obtaining a Type object for a specific type is to use the typeof operator shown here:

System.Type t1 = typeof(System.Text.StringBuilder);

The type name is not enclosed in quotes and must be resolvable by the compiler (meaning you must

reference the assembly using a compiler switch). Because the reference is resolved at compile time, the

assembly containing the type becomes a static dependency of your assembly and will be listed as such in

your assembly’s manifest.

An alternative to the typeof operator is the static method Type.GetType, which takes a string

containing the type name. Because you use a string to specify the type, you can vary it at runtime, which

opens the door to a world of dynamic programming opportunities using reflection (see recipe 3-12). If

you specify just the type name, the runtime must be able to locate the type in an already loaded

assembly. Alternatively, you can specify an assembly-qualified type name. Refer to the .NET Framework

SDK documentation for the Type.GetType method for a complete description of how to structure

assembly-qualified type names. Table 3-2 summarizes some other methods that provide access to Type

objects.



125



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

3-8. Pass Data Between Application Domains

Tải bản đầy đủ ngay(0 tr)

×