Megosztás a következőn keresztül:


Útmutató: A nyomtatási rendszer objektumtulajdonságainak lekérése tükröződés nélkül

A reflektáció használata az objektumok tulajdonságainak (és azok típusainak) felsorolására lelassíthatja az alkalmazás teljesítményét. A System.Printing.IndexedProperties névtér lehetővé teszi az információk tükröződés nélküli lekérését.

példa

Ennek lépései a következők.

  1. Hozzon létre egy ilyen típusú példányt. Az alábbi példában a típus a Microsoft .NET-keretrendszerrel rendelkező PrintQueue típus, de a PrintSystemObjectszármazó típusok esetében majdnem azonos kódnak kell működnie.

  2. Hozzon létre egy PrintPropertyDictionary a(z) PropertiesCollectiontípusból. A szótár minden bejegyzésének Value tulajdonsága egy olyan objektum, amely a PrintProperty-ből származó valamelyik típushoz tartozik.

  3. Sorolja fel a szótár tagjait. Mindegyiknél tegye a következőket.

  4. Emelje az egyes bejegyzések értékét a PrintProperty-ra, és használja egy PrintProperty objektum létrehozására.

  5. Az egyes Value objektumok PrintProperty típusának lekérése.


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

Lásd még