Anvisningar: Hämta egenskaper för utskriftssystemobjekt utan reflektion
Om du använder reflektion för att specificera egenskaperna (och typerna av dessa egenskaper) för ett objekt kan programmets prestanda försämras. Namnområdet System.Printing.IndexedProperties ger ett sätt att hämta den här informationen utan att använda reflektion.
Exempel
Stegen för att göra detta är följande.
Skapa en instans av typen. I exemplet nedan är typen den PrintQueue typ som levereras med Microsoft .NET Framework, men nästan identisk kod bör fungera för typer som du härleder från PrintSystemObject.
Skapa en PrintPropertyDictionary från typens PropertiesCollection. Egenskapen Value för varje post i den här ordlistan är ett objekt av en av de typer som härleds från PrintProperty.
Räkna upp medlemmarna i ordlistan. Gör följande för var och en av dem.
Omvandla värdet för varje post till PrintProperty och använd det för att skapa ett PrintProperty objekt.
Hämta typen av Value för vart och ett av PrintProperty objekt.
// 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()
Se även
.NET Desktop feedback