方法 : リフレクションを使用せずに印刷システム オブジェクトのプロパティを取得する
リフレクションを使用してオブジェクトのプロパティ (およびそれらのプロパティの型) を項目別に示すと、アプリケーションのパフォーマンスが低下する可能性があります。 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
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()
関連項目
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET Desktop feedback