Compartilhar via


Como obter propriedades de objeto do sistema de impressão sem reflexão

Usar a reflexão para itemizar as propriedades (e os tipos dessas propriedades) em um objeto pode diminuir o desempenho do aplicativo. O namespace System.Printing.IndexedProperties fornece um meio para obter essas informações sem usar reflexão.

Exemplo

As etapas para fazer isso são as seguintes.

  1. Crie uma instância do tipo. No exemplo abaixo, o tipo é o tipo PrintQueue que vem com o Microsoft .NET Framework, mas o código deve funcionar de forma quase idêntica para tipos que você deriva de PrintSystemObject.

  2. Crie um PrintPropertyDictionary com base no PropertiesCollectiondo tipo. A propriedade Value de cada entrada neste dicionário é um objeto de um dos tipos derivados de PrintProperty.

  3. Enumerar os membros do dicionário. Para cada um deles, faça o seguinte.

  4. Converta o valor de cada entrada para PrintProperty e use-o para criar um objeto PrintProperty.

  5. Obtenha o tipo da Value de cada objeto PrintProperty.


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


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

Consulte também