共用方式為


How to: Get Print System Object Properties Without Reflection

Using Reflection to itemize the properties (and the types of those properties) on an object can slow application performance. The System.Printing.IndexedProperties namespace provides a means to getting this information with using Reflection.

Example

The steps for doing this are as follows.

  1. Create an instance of the type. In the example below, the type is the PrintQueue type that ships with Microsoft .NET Framework version 3.0, but nearly identical code should work for types that you derive from PrintSystemObject.

  2. Create a PrintPropertyDictionary from the type's PropertiesCollection. The Value property of each entry in this dictionary is an object of one of the types derived from PrintProperty.

  3. Enumerate the members of the dictionary. For each of them, do the following.

  4. Up-cast the value of each entry to PrintProperty and use it to create a PrintProperty object.

  5. Get the type of the Value of each of the PrintProperty object.

// Enumerate the properties, and their types, of a queue without using Reflection
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

PrintPropertyDictionary printQueueProperties = defaultPrintQueue.PropertiesCollection;

Console.WriteLine("These are the properties, and their types, of {0}, a {1}", defaultPrintQueue.Name, defaultPrintQueue.GetType().ToString() +"\n");

foreach (DictionaryEntry entry in printQueueProperties)
{
    PrintProperty property = (PrintProperty)entry.Value;

    if (property.Value != null)
    {
        Console.WriteLine(property.Name + "\t(Type: {0})", property.Value.GetType().ToString());
    }
}
Console.WriteLine("\n\nPress Return to continue...");
Console.ReadLine();

See Also

Reference

PrintProperty
PrintSystemObject
System.Printing.IndexedProperties
PrintPropertyDictionary
LocalPrintServer
PrintQueue
DictionaryEntry

Concepts

Documents in Windows Presentation Foundation
Printing Overview

Other Resources

Printing Samples