次の方法で共有


Type.DefaultBinder プロパティ

既定のバインダへの参照を取得します。このバインダは、 InvokeMember によって呼び出される適切なメンバを選択するための内部規則を実装します。

Public Shared ReadOnly Property DefaultBinder As Binder
[C#]
public static Binder DefaultBinder {get;}
[C++]
public: __property static Binder* get_DefaultBinder();
[JScript]
public static function get DefaultBinder() : Binder;

プロパティ値

システムで使用される既定のバインダへの参照。

解説

共通言語ランタイムと共に提供される既定のバインダは、非常に特殊な場合を除くすべての状況で適用できます。用意されている既定のバインダの規則とは異なる規則に従うバインダが必要な場合は、 Binder クラスから派生した型を定義し、 InvokeMember オーバーロードのいずれかの binder パラメータを使用して、その型のインスタンスを渡します。

リフレクションは、共通の型システムにおけるアクセスに関する規則をモデル化します。たとえば、呼び出し元が同一アセンブリ内にある場合、呼び出し元に内部メンバに対する特別なアクセス許可は必要ありません。それ以外の場合は、呼び出し元に ReflectionPermission が必要です。このことは、プロテクト メンバ、プライベート メンバなどを検索する場合も同様です。

一般原則として、 ChangeType は型の拡大変換だけを実行します。この変換では、データは失われません。拡大変換の例としては、32 ビット符号付き整数値から 64 ビット符号付き整数値への変換があります。この変換は、データが失われる可能性がある縮小変換とは区別されます。型の縮小変換の例としては、64 ビット符号付き整数から 32 ビット符号付き整数への変換があります。

既定のバインダでサポートされる変換の一覧を次の表に示します。

変換元の型 変換後の型
任意の型 基本型
任意の型 実装するインターフェイス
Char Unt16、UInt32、Int32、UInt64、Int64、Single、Double
Byte Char、Unt16、Int16、UInt32、Int32、UInt64、Int64、Single、Double
SByte Int16、Int32、Int64、Single、Double
UInt16 UInt32、Int32、UInt64、Int64、Single、Double
Int16 Int32、Int64、Single、Double
UInt32 UInt64、Int64、Single、Double
Int32 Int64、Single、Double
UInt64 Single、Double
Int64 Single、Double
Single Double
非参照渡し 参照渡し

使用例

[Visual Basic, C#, C++] DefaultBinder プロパティから既定のバインダを取得し、 InvokeMember へのパラメータとして DefaultBinder の値を渡して、MyClass のメンバを実行する例を次に示します。

 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class MyDefaultBinderSample
    Public Shared Sub Main()
        Try
            Dim defaultBinder As Binder = Type.DefaultBinder
            Dim [myClass] As New [MyClass]()
            ' Invoke the HelloWorld method of MyClass.
            [myClass].GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, defaultBinder, [myClass], New Object() {})
        Catch e As Exception
            Console.WriteLine("Exception :" + e.Message.ToString())
        End Try
    End Sub 'Main

    Class [MyClass]

        Public Sub HelloWorld()
            Console.WriteLine("Hello World")
        End Sub 'HelloWorld
    End Class '[MyClass]
End Class 'MyDefaultBinderSample 

[C#] 
using System;
using System.Reflection;

public class MyDefaultBinderSample
{
    public static void Main()
    {
        try
        {
            Binder defaultBinder = Type.DefaultBinder;
            MyClass myClass = new MyClass();
            // Invoke the HelloWorld method of MyClass.
            myClass.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod,
                defaultBinder, myClass, new object [] {});
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception :" + e.Message);
        }
    }    

    class MyClass
    {
        public void HelloWorld()
        {
            Console.WriteLine("Hello World");
        }    
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

__gc class MyClass {
public:
   void HelloWorld() {
      Console::WriteLine(S"Hello World");
   }    
};

int main() {
   try {
      Binder*  defaultBinder = Type::DefaultBinder;
      MyClass* myClass = new MyClass();
      // Invoke the HelloWorld method of MyClass.
      myClass->GetType()->InvokeMember(S"HelloWorld", BindingFlags::InvokeMethod,
         defaultBinder, myClass, 0);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception : {0}", e->Message);
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Type クラス | Type メンバ | System 名前空間 | Binder