Type.DefaultBinder 屬性

定義

取得預設繫結器 (Binder) 的參考,它會實作內部規則來選取由 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]) 呼叫的適當成員。

public:
 static property System::Reflection::Binder ^ DefaultBinder { System::Reflection::Binder ^ get(); };
public static System.Reflection.Binder DefaultBinder { get; }
static member DefaultBinder : System.Reflection.Binder
Public Shared ReadOnly Property DefaultBinder As Binder

屬性值

系統使用的預設繫結器之參考。

範例

下列範例會從 DefaultBinder 屬性取得預設系結器,並將值當做參數傳遞 DefaultBinderInvokeMember ,以叫用 MyClass 的成員。

using namespace System;
using namespace System::Reflection;
ref class MyClass
{
public:
   void HelloWorld()
   {
      Console::WriteLine( "Hello World" );
   }

};

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

}
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");
        }	
    }
}
open System
open System.Reflection

type MyClass() =
    member _.HelloWorld() =
        printfn "Hello World"

try
    let defaultBinder = Type.DefaultBinder
    let myClass = MyClass()
    // Invoke the HelloWorld method of MyClass.
    myClass.GetType().InvokeMember("HelloWorld", BindingFlags.InvokeMethod, defaultBinder, myClass, [||])
    |> ignore
with e ->
    printfn $"Exception: {e.Message}"
Imports System.Reflection

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

    Class [MyClass]

        Public Sub HelloWorld()
            Console.WriteLine("Hello World")
        End Sub
    End Class
End Class

備註

Common Language Runtime 所提供的預設系結器適用于所有特製化的情況。 如果您需要遵循與所提供預設系結器不同之規則的系結器,請定義衍生自 Binder 類別的類型,並使用 binder 其中一個 InvokeMember 多載的參數傳遞該類型的實例。

反映會模型化一般型別系統的協助工具規則。 例如,如果呼叫端位於相同的元件中,則呼叫端不需要內部成員的特殊許可權。 否則,呼叫端需要 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
非參考 參考。

適用於

另請參閱