Tải bản đầy đủ - 0 (trang)
2-18. Remove Duplicate Items from an Array or Collection

2-18. Remove Duplicate Items from an Array or Collection

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

CHAPTER 2 ■ DATA MANIPULATION



new Fruit("fig", "brown"),

new Fruit("cranberry", "red"),

new Fruit("pear", "green")

};

// Use the Distinct method to remove duplicates

// and print out the unique entries that remain.

foreach (Fruit fruit in myList.Distinct(new FruitComparer()))

{

Console.WriteLine("Fruit: {0}:{1}", fruit.Name, fruit.Color);

}

// Wait to continue.

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

Console.ReadLine();

}

}

class FruitComparer : IEqualityComparer

{

public bool Equals(Fruit first, Fruit second)

{

return first.Name == second.Name && first.Color == second.Color;

}

public int GetHashCode(Fruit fruit)

{

return fruit.Name.GetHashCode() + fruit.Name.GetHashCode();

}

}

class Fruit

{

public Fruit(string nameVal, string colorVal)

{

Name = nameVal;

Color = colorVal;

}

public string Name { get; set; }

public string Color { get; set; }

}

}



101



www.it-ebooks.info



www.it-ebooks.info



CHAPTER 3

■■■



Application Domains, Reflection,

and Metadata

The power and flexibility of the Microsoft .NET Framework is enhanced by the ability to inspect and

manipulate types and metadata at runtime. The recipes in this chapter describe how to use application

domains, reflection, and metadata. Specifically, the recipes in this chapter describe how to do the

following:





Create application domains into which you can load assemblies that are isolated

from the rest of your application (recipe 3-1)







Create types that have the capability to cross application domain boundaries

(recipe 3-2) and types that are guaranteed to be unable to cross application

domain boundaries (recipe 3-4)







Control the loading of assemblies and the instantiation of types in local and

remote application domains (recipes 3-3, 3-5, 3-6, and 3-7)







Pass simple configuration data between application domains (recipe 3-8)







Unload application domains, which provides the only means through which you

can unload assemblies at runtime (recipe 3-9)







Inspect and test the type of an object using a variety of mechanisms built into the

C# language and capabilities provided by the objects themselves (recipes 3-10 and

3-11)







Dynamically instantiate an object and execute its methods at runtime using

reflection (recipe 3-12)







Create custom attributes (recipe 3-13), allowing you to associate metadata with

your program elements and inspect the value of those custom attributes at

runtime (recipe 3-14)







Use reflection to discover type members and to invoke a member at runtime

(recipes 3-15 and 3-16)



103



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA







Use dynamic types to simplify invoking a member using reflection (recipe 3-17)







Create custom dynamic types (recipe 3-18)



3-1. Create an Application Domain

Problem

You need to create a new application domain.



Solution

Use the static method CreateDomain of the System.AppDomain class.



How It Works

The simplest overload of the CreateDomain method takes a single string argument specifying a humanreadable name (friendly name) for the new application domain. Other overloads allow you to specify

evidence and configuration settings for the new application domain. You specify evidence using a

System.Security.Policy.Evidence object, and you specify configuration settings using a

System.AppDomainSetup object.

The AppDomainSetup class is a container of configuration information for an application domain.

Table 3-1 lists some of the properties of the AppDomainSetup class that you will use most often when

creating application domains. These properties are accessible after creation through members of the

AppDomain object. Some have different names, and some are modifiable at runtime; refer to the .NET

Framework’s software development kit (SDK) documentation on the AppDomain class for a

comprehensive discussion.

Table 3-1. Commonly Used AppDomainSetup Properties



Property



Description



ApplicationBase



The directory where the CLR will look during probing to resolve private

assemblies. (Recipe 3-5 discusses probing.) Effectively, ApplicationBase is

the root directory for the executing application. By default, this is the

directory containing the assembly. This is readable after creation using the

AppDomain.BaseDirectory property.



ConfigurationFile



The name of the configuration file used by code loaded into the application

domain. This is readable after creation using the AppDomain.GetData method

with the key APP_CONFIG_FILE. By default, the configuration file is stored in

the same folder as the application EXE file, but if you set ApplicationBase, it

will be in that same folder.



104



www.it-ebooks.info



CHAPTER 3 ■ APPLICATION DOMAINS, REFLECTION, AND METADATA



Property



Description



DisallowPublisherPolicy



Controls whether the publisher policy section of the application

configuration file is taken into consideration when determining which

version of a strongly named assembly to bind to. Recipe 3-5 discusses

publisher policy.



PrivateBinPath



A semicolon-separated list of directories that the runtime uses when

probing for private assemblies. These directories are relative to the directory

specified in ApplicationBase. This is readable after application domain

creation using the AppDomain.RelativeSearchPath property.



The Code

The following code demonstrates the creation and initial configuration of an application domain:

using System;

namespace Apress.VisualCSharpRecipes.Chapter03

{

class Recipe03_01

{

public static void Main()

{

// Instantiate an AppDomainSetup object.

AppDomainSetup setupInfo = new AppDomainSetup();

// Configure the application domain setup information.

setupInfo.ApplicationBase = @"C:\MyRootDirectory";

setupInfo.ConfigurationFile = "MyApp.config";

setupInfo.PrivateBinPath = "bin;plugins;external";

// Create a new application domain passing null as the evidence

// argument. Remember to save a reference to the new AppDomain as

// this cannot be retrieved any other way.

AppDomain newDomain =

AppDomain.CreateDomain("My New AppDomain",null, setupInfo);

// Wait to continue.

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

Console.ReadLine();

}

}

}



105



www.it-ebooks.info



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

2-18. Remove Duplicate Items from an Array or Collection

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

×