更新 : 2007 年 11 月
リフレクションを使用してオブジェクトのプロパティ (およびプロパティの型) を取得すると、アプリケーションのパフォーマンスが低下する可能性があります。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();
参照
概念
Windows Presentation Foundation のドキュメント
参照
System.Printing.IndexedProperties