次の方法で共有


方法 : リフレクションを使用せずに印刷システム オブジェクトのプロパティを取得する

更新 : 2007 年 11 月

リフレクションを使用してオブジェクトのプロパティ (およびプロパティの型) を取得すると、アプリケーションのパフォーマンスが低下する可能性があります。System.Printing.IndexedProperties 名前空間には、リフレクションを使用してこの情報を取得するための手段が用意されています。

使用例

実行手順は次のとおりです。

  1. 型のインスタンスを作成します。次の例では、型は Microsoft .NET Framework で提供されている PrintQueue 型ですが、PrintSystemObject から派生した型に対しても、ほぼ同じコードを使用できます。

  2. 型の PropertiesCollection から PrintPropertyDictionary を作成します。このディクショナリの各エントリの Value プロパティは、PrintProperty から派生した型のオブジェクトです。

  3. ディクショナリのメンバを列挙します。各メンバに対し、以下を行います。

  4. 各エントリの値を PrintProperty にアップキャストし、それを使用して PrintProperty オブジェクトを作成します。

  5. 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 のドキュメント

印刷の概要

参照

PrintProperty

PrintSystemObject

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

その他の技術情報

印刷のサンプル