HOW TO:取得列印系統物件屬性但不使用反映
使用反映將物件上的屬性 (和這些屬性的型別) 項目化,會降低應用程式的效能。 System.Printing.IndexedProperties 命名空間提供了一種利用反映取得這項資訊的方法。
範例
這項作業的步驟如下。
建立型別的執行個體。 在下列範例中,這個型別是隨 Microsoft .NET Framework 提供的 PrintQueue,但是相同的程式碼只要做少許修改,應該也適用於您從 PrintSystemObject 衍生而來的型別。
從型別的 PropertiesCollection 建立 PrintPropertyDictionary。 在這個字典中,每個項目的 Value 屬性各代表從 PrintProperty 衍生的其中一個型別的物件。
列舉字典的成員。 針對每一個成員執行下列步驟。
將每個項目值向上轉型為 PrintProperty,並用它建立 PrintProperty 物件。
取得 PrintProperty 物件的各 Value 的型別。
' 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();
請參閱
參考
System.Printing.IndexedProperties