0
|
1 using System;
|
|
2 using System.Globalization;
|
|
3 using System.Reflection;
|
|
4
|
|
5 namespace BLToolkit.Reflection
|
|
6 {
|
|
7 /// <Summary>
|
|
8 /// Selects a member from a list of candidates, and performs type conversion
|
|
9 /// from actual argument type to formal argument type.
|
|
10 /// </Summary>
|
|
11 [Serializable]
|
|
12 public class GenericBinder : Binder
|
|
13 {
|
|
14 private readonly bool _genericMethodDefinition;
|
|
15 public GenericBinder(bool genericMethodDefinition)
|
|
16 {
|
|
17 _genericMethodDefinition = genericMethodDefinition;
|
|
18 }
|
|
19
|
|
20 #region System.Reflection.Binder methods
|
|
21
|
|
22 public override MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args,
|
|
23 ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state)
|
|
24 {
|
|
25 throw new InvalidOperationException("GenericBinder.BindToMethod");
|
|
26 }
|
|
27
|
|
28 public override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture)
|
|
29 {
|
|
30 throw new InvalidOperationException("GenericBinder.BindToField");
|
|
31 }
|
|
32
|
|
33 public override MethodBase SelectMethod(
|
|
34 BindingFlags bindingAttr,
|
|
35 MethodBase[] matchMethods,
|
|
36 Type[] parameterTypes,
|
|
37 ParameterModifier[] modifiers)
|
|
38 {
|
|
39 for (int i = 0; i < matchMethods.Length; ++i)
|
|
40 {
|
|
41 if (matchMethods[i].IsGenericMethodDefinition != _genericMethodDefinition)
|
|
42 continue;
|
|
43
|
|
44 ParameterInfo[] pis = matchMethods[i].GetParameters();
|
|
45 bool match = (pis.Length == parameterTypes.Length);
|
|
46
|
|
47 for (int j = 0; match && j < pis.Length; ++j)
|
|
48 {
|
|
49 match = TypeHelper.CompareParameterTypes(pis[j].ParameterType, parameterTypes[j]);
|
|
50 }
|
|
51
|
|
52 if (match)
|
|
53 return matchMethods[i];
|
|
54 }
|
|
55
|
|
56 return null;
|
|
57 }
|
|
58
|
|
59 public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType,
|
|
60 Type[] indexes, ParameterModifier[] modifiers)
|
|
61 {
|
|
62 throw new InvalidOperationException("GenericBinder.SelectProperty");
|
|
63 }
|
|
64
|
|
65 public override object ChangeType(object value, Type type, CultureInfo culture)
|
|
66 {
|
|
67 throw new InvalidOperationException("GenericBinder.ChangeType");
|
|
68 }
|
|
69
|
|
70 public override void ReorderArgumentArray(ref object[] args, object state)
|
|
71 {
|
|
72 throw new InvalidOperationException("GenericBinder.ReorderArgumentArray");
|
|
73 }
|
|
74
|
|
75 #endregion
|
|
76
|
|
77 private static GenericBinder _generic;
|
|
78 public static GenericBinder Generic
|
|
79 {
|
|
80 get { return _generic ?? (_generic = new GenericBinder(true)); }
|
|
81 }
|
|
82
|
|
83 private static GenericBinder _nonGeneric;
|
|
84 public static GenericBinder NonGeneric
|
|
85 {
|
|
86 get { return _nonGeneric ?? (_nonGeneric = new GenericBinder(false)); }
|
|
87 }
|
|
88 }
|
|
89 }
|