Type.DefaultBinder Type.DefaultBinder Type.DefaultBinder Type.DefaultBinder Property

定義

取得預設繫結器 (Binder) 的參考,它會實作內部規則來選取由 InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]) 呼叫的適當成員。Gets a reference to the default binder, which implements internal rules for selecting the appropriate members to be called by 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; }
member this.DefaultBinder : System.Reflection.Binder
Public Shared ReadOnly Property DefaultBinder As Binder

屬性值

系統使用的預設繫結器之參考。A reference to the default binder used by the system.

範例

下列範例會從DefaultBinder屬性取得預設的系結器, 並藉由將DefaultBinder值當做參數傳遞給InvokeMember, 叫用 MyClass 的成員。The following example gets the default binder from the DefaultBinder property, and invokes a member of MyClass by passing the DefaultBinder value as a parameter to InvokeMember.

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");
        }	
    }
}
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 

備註

通用語言執行時間提供的預設系結器適用于所有但最特殊的情況。The default binder provided with the common language runtime is applicable in all but the most specialized circumstances. 如果您需要的系結器遵循與所提供之預設系結器不同的規則, 請定義衍生自Binder類別的類型, 並binder使用其中一個多InvokeMember載的參數來傳遞該類型的實例。If you need a binder that follows rules that differ from those of the supplied default binder, define a type derived from the Binder class and pass an instance of that type using the binder parameter of one of the InvokeMember overloads.

反映會模型一般型別系統的存取範圍規則。Reflection models the accessibility rules of the common type system. 例如, 如果呼叫端在相同的元件中, 則呼叫端不需要內部成員的特殊許可權。For example, if the caller is in the same assembly, the caller does not need special permissions for internal members. 否則, 呼叫端需要ReflectionPermissionOtherwise, the caller needs ReflectionPermission. 這與查閱受保護、私用等等的成員一致。This is consistent with lookup of members that are protected, private, and so on.

一般的原則是ChangeType , 應該只執行擴輾轉換, 這永遠不會遺失資料。The general principle is that ChangeType should perform only widening conversions, which never lose data. 擴輾轉換的範例是將32位帶正負號的整數值轉換成64位帶正負號整數的值。An example of a widening conversion is converting a value that is a 32-bit signed integer to a value that is a 64-bit signed integer. 這可區別縮小轉換, 這可能會遺失資料。This is distinguished from a narrowing conversion, which may lose data. 縮小轉換的範例是將64位帶正負號的整數轉換成32位帶正負號的整數。An example of a narrowing conversion is converting a 64-bit signed integer to a 32-bit signed integer.

下表列出預設系結器支援的轉換。The following table lists the conversions supported by the default binder.

來源類型Source Type 目標型別Target Type
任何型別Any type 其基底類型。Its base type.
任何型別Any type 它所實行的介面。The interface it implements.
CharChar Unt16、UInt32、Int32、UInt64、Int64、Single、DoubleUnt16, UInt32, Int32, UInt64, Int64, Single, Double
ByteByte Char、Unt16、Int16、UInt32、Int32、UInt64、Int64、Single、DoubleChar, Unt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double
SByteSByte Int16、Int32、Int64、Single、DoubleInt16, Int32, Int64, Single, Double
UInt16UInt16 UInt32、Int32、UInt64、Int64、Single、DoubleUInt32, Int32, UInt64, Int64, Single, Double
Int16Int16 Int32、Int64、Single、DoubleInt32, Int64, Single, Double
UInt32UInt32 UInt64、Int64、Single、DoubleUInt64, Int64, Single, Double
Int32Int32 Int64、Single、DoubleInt64, Single, Double
UInt64UInt64 Single、DoubleSingle, Double
Int64Int64 Single、DoubleSingle, Double
SingleSingle DoubleDouble
非參考Non-reference 傳址方式。By-reference.

適用於

另請參閱