Udostępnij za pośrednictwem


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, 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
            Dim localPrintServer As New LocalPrintServer()
            Dim defaultPrintQueue As PrintQueue = LocalPrintServer.GetDefaultPrintQueue()

            Dim printQueueProperties As PrintPropertyDictionary = defaultPrintQueue.PropertiesCollection

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

            For Each entry As DictionaryEntry In printQueueProperties
                Dim [property] As PrintProperty = CType(entry.Value, PrintProperty)

                If [property].Value IsNot Nothing Then
                    Console.WriteLine([property].Name & vbTab & "(Type: {0})", [property].Value.GetType().ToString())
                End If
            Next entry
            Console.WriteLine(vbLf & vbLf & "Press Return to continue...")
            Console.ReadLine()


            // 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 WPF

Printing Overview