T_Cyjb_Reflection_PowerBinder - CYJB/Cyjb GitHub Wiki
从候选者列表中选择一个成员,并执行实参类型到形参类型的类型转换。 在选择时,支持对泛型方法进行选择,并允许进行强制类型转换。
System.Object
System.Reflection.Binder
Cyjb.Reflection.PowerBinder
Namespace: Cyjb.Reflection
Assembly: Cyjb (in Cyjb.dll) Version: 1.0.23+7750dd8e971297c5fa962a3bee37fb78f72793f6
C#
[SerializableAttribute]
public sealed class PowerBinder : Binder
The PowerBinder type exposes the following members.
名称 | 说明 | |
---|---|---|
![]() ![]() |
Default | 获取默认的 PowerBinder 实例。 |
![]() ![]() |
Explicit | 获取可以进行显式类型转换的 PowerBinder 实例。 |
Back to Top
PowerBinder 类是对 Binder 类的扩展, 支持泛型方法和强制类型转换,可以有效的扩展反射得到类型成员的过程。
关于 Binder 类的原理,以及 PowerBinder 类的实现,可以参见我的博文 《C# 使用 Binder 类自定义反射》。
关于进行泛型类型推断的原理,可以参考我的博文 《C# 泛型方法的类型推断》。
下面的例子演示了 PowerBinder 对泛型方法和强制类型转换的支持。
class TestClass {
public static void TestMethod(int value) { }
public static void TestMethod2<T>(T value) { }
}
Type outputType = typeof(TestClass);
Console.WriteLine(outputType.GetMethod("TestMethod", new Type[] { typeof(long) }));
Console.WriteLine(outputType.GetMethod("TestMethod", BindingFlags.Static | BindingFlags.Public,
PowerBinder.CastBinder, new Type[] { typeof(long) }, null));
Console.WriteLine(outputType.GetMethod("TestMethod2", new Type[] { typeof(string) }));
Console.WriteLine(outputType.GetMethod("TestMethod2", BindingFlags.Static | BindingFlags.Public,
PowerBinder.DefaultBinder, new Type[] { typeof(string) }, null));
《C# 泛型方法的类型推断》