动态装载和使用类型(2)
点击次数:28 次 发布日期:2008-11-26 12:32:42 作者:源代码网
|
源代码网推荐 public class CustomBinderDriver 源代码网推荐 { 源代码网推荐 public static void Main (string[] arguments) 源代码网推荐 { 源代码网推荐 Type t = typeof (CustomBinderDriver); 源代码网推荐 CustomBinder binder = new CustomBinder(); 源代码网推荐 BindingFlags flags = BindingFlags.InvokeMethod|BindingFlags.Instance| 源代码网推荐 BindingFlags.Public|BindingFlags.Static; 源代码网推荐 源代码网推荐 //Case 1. Neither argument coercion nor memberselection is needed. 源代码网推荐 args = new Object[] {}; 源代码网推荐 t.InvokeMember ("PrintBob", flags,binder, null, args); 源代码网推荐 源代码网推荐 //Case 2. Only member selection is needed. 源代码网推荐 args = new Object[] {42}; 源代码网推荐 t.InvokeMember ("PrintValue", flags,binder, null, args); 源代码网推荐 源代码网推荐 //Case 3. Only argument coercion is needed. 源代码网推荐 args = new Object[] {"5.5"}; 源代码网推荐 t.InvokeMember ("PrintNumber",flags, binder, null, args); 源代码网推荐 } 源代码网推荐 源代码网推荐 public static void PrintBob () 源代码网推荐 { 源代码网推荐 Console.WriteLine ("PrintBob"); 源代码网推荐 } 源代码网推荐 源代码网推荐 public static void PrintValue (long value) 源代码网推荐 { 源代码网推荐 Console.WriteLine ("PrintValue ({0})",value); 源代码网推荐 } 源代码网推荐 public static void PrintValue (String value) 源代码网推荐 { 源代码网推荐 Console.WriteLine ("PrintValue"{0}")",value); 源代码网推荐 } 源代码网推荐 源代码网推荐 public static void PrintNumber (double value) 源代码网推荐 { 源代码网推荐 Console.WriteLine ("PrintNumber ({0})",value); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 当存在多于一个的同名成员时,就需要有重载解析。Binder.BindToMethod 和Binder.BindToField 方法可以用来绑定到一个成员。Binder.BindToMethod也可以通过get 和set 属性访问器提供属性解析。 源代码网推荐 源代码网推荐 BindToMethod 返回可被调用的MethodBase. 如无可用的调用则返回null. 如果无法调用,BindToMethod 返回 MethodBase 为 调用或 null。MethodBase返回值无需是match参数之一,尽管事实往往如此。 源代码网推荐 源代码网推荐 调用者 也许会想得到ByRef 参数的返回。所以,如果BindTo方法改动过参数数组,Binder 允许客户使参数数组映射回它原来的表格。为了实现这点,调用者必须确保参数顺序不变。当参数由名字传递,Binder重新整理参数组,以供调用者察看。 源代码网推荐 源代码网推荐 可用成员是指那些在类型或任何基本类型中定义的那些成员。如果指明BindingFlags.NonPublic,任何访问级别的成员都会在返回中。如果BindingFlags.NonPublic 没有被指明,binder必须执行访问规则。当指明Public或 NonPublic 绑定标志, 你必须也指明Instance 或Static 标志, 否则不会有成员返回。 源代码网推荐 源代码网推荐 如果只有一个成员与名字对应,就不需要回调,也就完成到这个方法的绑定。Case 1 中的代码例子表明了这一点:只有一个可用的PrintBob 方法, 所以,不需要回调。 源代码网推荐 源代码网推荐 如在可用集中,有多于一个成员。所有这些方法被传递给BindTo方法, 再由它选择适当的方法,并且返回。在 Case 2 中的代码例子中,有两种叫做PrintValue的方法。合适的方法取决于对BindToMethod调用。 源代码网推荐 源代码网推荐 ChangeType 执行参数转换, 它把实际参数转变为选定方法的参数类型。即使类型已经完美匹配,ChangeType也会针对每个参数被调用。 源代码网推荐 源代码网推荐 在 Case 3 中的代码例子中, 值为"5.5"的String类型的一个实际参数以正式参数Double类型被传递给方法。要想调用成功,字符串值"5.5"必须被转变为一个double值。ChangeType 执行了这种转变。 源代码网推荐 源代码网推荐 ChangeType 仅执行无损失转换, 如下表所示: 源代码网推荐 Source Type Target Type 源代码网推荐 Any type Its base type 源代码网推荐 Any type Interface it implements 源代码网推荐 Char UInt16, UInt32, Int32, UInt64, Int64, Single, Double 源代码网推荐 Byte Char, UInt16, 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 Single, Double 源代码网推荐 Int64 Single, Double 源代码网推荐 Single Double 源代码网推荐 Non-reference type Reference type 源代码网推荐 源代码网推荐 Type类有Get方法,可使用Binder类型的参数的来解析对某成员的引用。Type.GetConstructor,Type. GetMethod , 和 Type.GetProperty 通过提供某成员的签名信息来查找该成员。它们调用Binder.SelectMethod和Binder.SelectProperty 以选择适合方法的签名信息。 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
