次の方法で共有


方法: リフレクションなしで印刷システム オブジェクトのプロパティを取得する

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

これを行う手順は次のとおりです。

  1. 型のインスタンスを作成します。 次の例では、型は Microsoft .NET Framework に付属する PrintQueue 型ですが、 PrintSystemObjectから派生した型では、ほぼ同じコードが機能する必要があります。

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

  3. ディクショナリのメンバーを列挙します。 それぞれに対して、次の操作を行います。

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

  5. Value オブジェクトのPrintPropertyの型を取得します。


// 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()

こちらも参照ください