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属性获取默认联编程序,并通过将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.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 '[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.

适用于

另请参阅