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 System.Printing.IndexedProperties namespace 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 a seguir, o tipo é o disponibilizado com o PrintQueue Microsoft .NET Framework, mas o código quase idêntico deve funcionar para tipos que você deriva de PrintSystemObject.

  2. Crie um PrintPropertyDictionary a partir do PropertiesCollectiontipo. A Value propriedade 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 e use-o PrintProperty para criar um PrintProperty objeto.

  5. Obtenha o tipo de Value de cada um dos PrintProperty objetos.


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