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

リフレクションを使用してオブジェクトのプロパティ (およびプロパティの型) を取得すると、アプリケーションのパフォーマンスが低下する可能性があります。 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
            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();

参照

参照

PrintProperty

PrintSystemObject

System.Printing.IndexedProperties

PrintPropertyDictionary

LocalPrintServer

PrintQueue

DictionaryEntry

概念

WPF のドキュメント

印刷の概要