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

定义

获取默认联编程序的引用,该程序实现的内部规则用于选择由 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属性,并表示通过调用成员 MyClassDefaultBinder作为参数的值InvokeMemberThe 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. 扩大转换的示例将转换为 64 位有符号的整数的值将 32 位有符号的整数的值。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.

适用于

另请参阅