Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
Použití reflexe k vypsání vlastností (a typů daných vlastností) v rámci objektu může zpomalit výkon aplikace. Obor System.Printing.IndexedProperties názvů poskytuje způsob získání těchto informací bez použití reflexe.
Příklad
Postup je následující.
Vytvořte instanci typu. V následujícím příkladu je typ PrintQueue, který je součástí rozhraní Microsoft .NET Framework, ale téměř stejný kód by měl fungovat i pro typy, které odvodíte z PrintSystemObject.
Vytvořte PrintPropertyDictionary z typu PropertiesCollection. Vlastnost Value každé položky v tomto slovníku je objekt jednoho z typů odvozených z PrintProperty.
Vytvoří výčet členů slovníku. Pro každou z nich proveďte následující kroky.
Povýšte datový typ každé položky na PrintProperty a použijte jej k vytvoření objektu PrintProperty.
Získejte typ Value každého objektu 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()
Viz také
.NET Desktop feedback