Type.DefaultBinder 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取对默认绑定程序的引用,该绑定器实现内部规则以选择要调用的 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 属性获取默认绑定器,并通过将 DefaultBinder 值作为参数 InvokeMember传递给 MyClass 调用成员。
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
注解
与公共语言运行时一起提供的默认绑定器适用于所有专用环境。 如果需要遵循与提供的默认绑定器不同的规则的绑定器,请定义派生自 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 | 单、双 |
| Int64 | 单、双 |
| Single | Double |
| 非引用 | 按引用。 |