Практическое руководство. Получение свойств объекта системы печати без отражения

Использование отражения для перечисления свойств (и типов этих свойств) в объекте может уменьшить скорость работы приложения. Пространство имен System.Printing.IndexedProperties позволяет извлечь эту информации с помощью отражения.

Пример

Ниже приведены шаги, необходимые для выполнения этой операции.

  1. Создайте экземпляр типа. В примере, приведенном ниже, тип является типом PrintQueue, поставляемым вместе с Microsoft .NET Framework. Однако почти идентичный этому код должен работать для типов, производных от PrintSystemObject.

  2. Создайте PrintPropertyDictionary из PropertiesCollection типа. Свойство Value каждой записи в этом словаре является объектом одного из типов, производных от PrintProperty.

  3. Перечислите члены словаря. Для каждого из них выполните следующие действия.

  4. Приведите значение каждого элемента в PrintProperty и используйте его для создания объекта PrintProperty.

  5. Получите тип Value каждого объекта PrintProperty.


            ' 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();

См. также

Ссылки

PrintProperty

PrintSystemObject

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

Основные понятия

Документы в WPF

Общие сведения о печати