0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Collections.ObjectModel;
|
|
4 using System.Data;
|
|
5 using System.Data.SqlTypes;
|
|
6 using System.IO;
|
|
7 using System.Linq;
|
|
8 using System.Linq.Expressions;
|
|
9 using System.Reflection;
|
|
10 using System.Xml;
|
|
11 #if !SILVERLIGHT
|
|
12 using System.Xml.Linq;
|
|
13 #endif
|
|
14
|
|
15 using LinqBinary = System.Data.Linq.Binary;
|
|
16 using BLToolkit.Reflection;
|
|
17
|
|
18 namespace BLToolkit.Data.Linq
|
|
19 {
|
|
20 public class ReflectionHelper
|
|
21 {
|
|
22 public class Expressor<T>
|
|
23 {
|
|
24 public static FieldInfo FieldExpressor(Expression<Func<T,object>> func)
|
|
25 {
|
|
26 return (FieldInfo)((MemberExpression)((UnaryExpression)func.Body).Operand).Member;
|
|
27 }
|
|
28
|
|
29 public static MethodInfo PropertyExpressor(Expression<Func<T,object>> func)
|
|
30 {
|
|
31 return ((PropertyInfo)((MemberExpression)func.Body).Member).GetGetMethod();
|
|
32 }
|
|
33
|
|
34 public static MethodInfo MethodExpressor(Expression<Func<T,object>> func)
|
|
35 {
|
|
36 var ex = func.Body;
|
|
37
|
|
38 if (ex is UnaryExpression)
|
|
39 ex = ((UnaryExpression)ex).Operand;
|
|
40
|
|
41 //if (ex is MemberExpression)
|
|
42 // return ((PropertyInfo)((MemberExpression)ex).Member).GetGetMethod();
|
|
43
|
3
|
44 /// HACK
|
4
|
45 return ExtractMethodInfo ((MethodCallExpression)ex);
|
0
|
46 }
|
|
47 }
|
|
48
|
|
49 public static MemberInfo MemeberInfo(LambdaExpression func)
|
|
50 {
|
|
51 var ex = func.Body;
|
|
52
|
|
53 if (ex is UnaryExpression)
|
|
54 ex = ((UnaryExpression)ex).Operand;
|
|
55
|
|
56 return
|
|
57 ex is MemberExpression ? ((MemberExpression) ex).Member :
|
3
|
58 ex is MethodCallExpression ? ExtractMethodInfo((MethodCallExpression)ex) :
|
0
|
59 (MemberInfo)((NewExpression) ex).Constructor;
|
|
60 }
|
|
61
|
3
|
62 public static MethodInfo ExtractMethodInfo(MethodCallExpression mce) {
|
|
63 var mi = mce.Method;
|
4
|
64 if (mi.IsVirtual && mi.DeclaringType != mce.Object.Type && !mce.Object.Type.IsInterface)
|
3
|
65 mi = mce.Object.Type.GetMethod(mi.Name, mi.GetParameters().Select(p => p.ParameterType).ToArray());
|
|
66
|
|
67 return mi;
|
|
68 }
|
|
69
|
0
|
70 public class Binary : Expressor<BinaryExpression>
|
|
71 {
|
|
72 public static MethodInfo Conversion = PropertyExpressor(e => e.Conversion);
|
|
73 public static MethodInfo Left = PropertyExpressor(e => e.Left);
|
|
74 public static MethodInfo Right = PropertyExpressor(e => e.Right);
|
|
75 }
|
|
76
|
|
77 public class Unary : Expressor<UnaryExpression>
|
|
78 {
|
|
79 public static MethodInfo Operand = PropertyExpressor(e => e.Operand);
|
|
80 }
|
|
81
|
|
82 public class LambdaExpr : Expressor<LambdaExpression>
|
|
83 {
|
|
84 public static MethodInfo Body = PropertyExpressor(e => e.Body);
|
|
85 public static MethodInfo Parameters = PropertyExpressor(e => e.Parameters);
|
|
86 }
|
|
87
|
|
88 public class Constant : Expressor<ConstantExpression>
|
|
89 {
|
|
90 public static MethodInfo Value = PropertyExpressor(e => e.Value);
|
|
91 }
|
|
92
|
|
93 public class QueryableInt : Expressor<IQueryable>
|
|
94 {
|
|
95 public static MethodInfo Expression = PropertyExpressor(e => e.Expression);
|
|
96 }
|
|
97
|
|
98 public class MethodCall : Expressor<MethodCallExpression>
|
|
99 {
|
|
100 public static MethodInfo Object = PropertyExpressor(e => e.Object);
|
|
101 public static MethodInfo Arguments = PropertyExpressor(e => e.Arguments);
|
|
102 }
|
|
103
|
|
104 public class Conditional : Expressor<ConditionalExpression>
|
|
105 {
|
|
106 public static MethodInfo Test = PropertyExpressor(e => e.Test);
|
|
107 public static MethodInfo IfTrue = PropertyExpressor(e => e.IfTrue);
|
|
108 public static MethodInfo IfFalse = PropertyExpressor(e => e.IfFalse);
|
|
109 }
|
|
110
|
|
111 public class Invocation : Expressor<InvocationExpression>
|
|
112 {
|
|
113 public static MethodInfo Expression = PropertyExpressor(e => e.Expression);
|
|
114 public static MethodInfo Arguments = PropertyExpressor(e => e.Arguments);
|
|
115 }
|
|
116
|
|
117 public class ListInit : Expressor<ListInitExpression>
|
|
118 {
|
|
119 public static MethodInfo NewExpression = PropertyExpressor(e => e.NewExpression);
|
|
120 public static MethodInfo Initializers = PropertyExpressor(e => e.Initializers);
|
|
121 }
|
|
122
|
|
123 public class ElementInit : Expressor<System.Linq.Expressions.ElementInit>
|
|
124 {
|
|
125 public static MethodInfo Arguments = PropertyExpressor(e => e.Arguments);
|
|
126 }
|
|
127
|
|
128 public class Member : Expressor<MemberExpression>
|
|
129 {
|
|
130 public static MethodInfo Expression = PropertyExpressor(e => e.Expression);
|
|
131 }
|
|
132
|
|
133 public class MemberInit : Expressor<MemberInitExpression>
|
|
134 {
|
|
135 public static MethodInfo NewExpression = PropertyExpressor(e => e.NewExpression);
|
|
136 public static MethodInfo Bindings = PropertyExpressor(e => e.Bindings);
|
|
137 }
|
|
138
|
|
139 public class New : Expressor<NewExpression>
|
|
140 {
|
|
141 public static MethodInfo Arguments = PropertyExpressor(e => e.Arguments);
|
|
142 }
|
|
143
|
|
144 public class NewArray : Expressor<NewArrayExpression>
|
|
145 {
|
|
146 public static MethodInfo Expressions = PropertyExpressor(e => e.Expressions);
|
|
147 }
|
|
148
|
|
149 public class TypeBinary : Expressor<TypeBinaryExpression>
|
|
150 {
|
|
151 public static MethodInfo Expression = PropertyExpressor(e => e.Expression);
|
|
152 }
|
|
153
|
|
154 public class IndexExpressor<T>
|
|
155 {
|
|
156 public static MethodInfo IndexerExpressor(Expression<Func<ReadOnlyCollection<T>, object>> func)
|
|
157 {
|
|
158 return ((MethodCallExpression)((UnaryExpression)func.Body).Operand).Method;
|
|
159 }
|
|
160
|
|
161 public static MethodInfo Item = IndexerExpressor(c => c[0]);
|
|
162 }
|
|
163
|
|
164 public class MemberAssignmentBind : Expressor<MemberAssignment>
|
|
165 {
|
|
166 public static MethodInfo Expression = PropertyExpressor(e => e.Expression);
|
|
167 }
|
|
168
|
|
169 public class MemberListBind : Expressor<MemberListBinding>
|
|
170 {
|
|
171 public static MethodInfo Initializers = PropertyExpressor(e => e.Initializers);
|
|
172 }
|
|
173
|
|
174 public class MemberMemberBind : Expressor<MemberMemberBinding>
|
|
175 {
|
|
176 public static MethodInfo Bindings = PropertyExpressor(e => e.Bindings);
|
|
177 }
|
|
178
|
|
179 #if FW4 || SILVERLIGHT
|
|
180
|
|
181 public class Block : Expressor<BlockExpression>
|
|
182 {
|
|
183 public static MethodInfo Expressions = PropertyExpressor(e => e.Expressions);
|
|
184 public static MethodInfo Variables = PropertyExpressor(e => e.Variables);
|
|
185 }
|
|
186
|
|
187 #endif
|
|
188
|
|
189 public static MethodInfo ExprItem = IndexExpressor<Expression> .Item;
|
|
190 public static MethodInfo ParamItem = IndexExpressor<ParameterExpression>.Item;
|
|
191 public static MethodInfo ElemItem = IndexExpressor<ElementInit> .Item;
|
|
192
|
|
193 public class DataReader : Expressor<IDataReader>
|
|
194 {
|
|
195 public static MethodInfo GetValue = MethodExpressor(rd => rd.GetValue(0));
|
|
196 public static MethodInfo IsDBNull = MethodExpressor(rd => rd.IsDBNull(0));
|
|
197 }
|
|
198
|
|
199 internal class QueryCtx : Expressor<QueryContext>
|
|
200 {
|
|
201 public static FieldInfo Counter = FieldExpressor(ctx => ctx.Counter);
|
|
202 }
|
|
203
|
|
204 public class MapSchema : Expressor<Mapping.MappingSchema>
|
|
205 {
|
|
206 public static MethodInfo MapValueToEnum = MethodExpressor(m => m.MapValueToEnum(null, (Type)null));
|
|
207 public static MethodInfo MapValueToEnumWithMemberAccessor = MethodExpressor(m => m.MapValueToEnum(null, (MemberAccessor)null));
|
|
208 public static MethodInfo ChangeType = MethodExpressor(m => m.ConvertChangeType(null, null));
|
|
209
|
|
210 public static Dictionary<Type,MethodInfo> Converters = new Dictionary<Type,MethodInfo>
|
|
211 {
|
|
212 // Primitive Types
|
|
213 //
|
|
214 { typeof(SByte), MethodExpressor(m => m.ConvertToSByte (null)) },
|
|
215 { typeof(Int16), MethodExpressor(m => m.ConvertToInt16 (null)) },
|
|
216 { typeof(Int32), MethodExpressor(m => m.ConvertToInt32 (null)) },
|
|
217 { typeof(Int64), MethodExpressor(m => m.ConvertToInt64 (null)) },
|
|
218 { typeof(Byte), MethodExpressor(m => m.ConvertToByte (null)) },
|
|
219 { typeof(UInt16), MethodExpressor(m => m.ConvertToUInt16 (null)) },
|
|
220 { typeof(UInt32), MethodExpressor(m => m.ConvertToUInt32 (null)) },
|
|
221 { typeof(UInt64), MethodExpressor(m => m.ConvertToUInt64 (null)) },
|
|
222 { typeof(Char), MethodExpressor(m => m.ConvertToChar (null)) },
|
|
223 { typeof(Single), MethodExpressor(m => m.ConvertToSingle (null)) },
|
|
224 { typeof(Double), MethodExpressor(m => m.ConvertToDouble (null)) },
|
|
225 { typeof(Boolean), MethodExpressor(m => m.ConvertToBoolean (null)) },
|
|
226
|
|
227 // Simple Types
|
|
228 //
|
|
229 { typeof(String), MethodExpressor(m => m.ConvertToString (null)) },
|
|
230 { typeof(DateTime), MethodExpressor(m => m.ConvertToDateTime (null)) },
|
|
231 { typeof(TimeSpan), MethodExpressor(m => m.ConvertToTimeSpan (null)) },
|
|
232 { typeof(DateTimeOffset), MethodExpressor(m => m.ConvertToDateTimeOffset (null)) },
|
|
233 { typeof(Decimal), MethodExpressor(m => m.ConvertToDecimal (null)) },
|
|
234 { typeof(Guid), MethodExpressor(m => m.ConvertToGuid (null)) },
|
|
235 { typeof(Stream), MethodExpressor(m => m.ConvertToStream (null)) },
|
|
236 #if !SILVERLIGHT
|
|
237 { typeof(XmlReader), MethodExpressor(m => m.ConvertToXmlReader (null)) },
|
|
238 { typeof(XmlDocument), MethodExpressor(m => m.ConvertToXmlDocument (null)) },
|
|
239 { typeof(XElement), MethodExpressor(m => m.ConvertToXElement (null)) },
|
|
240 #endif
|
|
241 { typeof(Byte[]), MethodExpressor(m => m.ConvertToByteArray (null)) },
|
|
242 { typeof(LinqBinary), MethodExpressor(m => m.ConvertToLinqBinary (null)) },
|
|
243 { typeof(Char[]), MethodExpressor(m => m.ConvertToCharArray (null)) },
|
|
244
|
|
245 // Nullable Types
|
|
246 //
|
|
247 { typeof(SByte?), MethodExpressor(m => m.ConvertToNullableSByte (null)) },
|
|
248 { typeof(Int16?), MethodExpressor(m => m.ConvertToNullableInt16 (null)) },
|
|
249 { typeof(Int32?), MethodExpressor(m => m.ConvertToNullableInt32 (null)) },
|
|
250 { typeof(Int64?), MethodExpressor(m => m.ConvertToNullableInt64 (null)) },
|
|
251 { typeof(Byte?), MethodExpressor(m => m.ConvertToNullableByte (null)) },
|
|
252 { typeof(UInt16?), MethodExpressor(m => m.ConvertToNullableUInt16 (null)) },
|
|
253 { typeof(UInt32?), MethodExpressor(m => m.ConvertToNullableUInt32 (null)) },
|
|
254 { typeof(UInt64?), MethodExpressor(m => m.ConvertToNullableUInt64 (null)) },
|
|
255 { typeof(Char?), MethodExpressor(m => m.ConvertToNullableChar (null)) },
|
|
256 { typeof(Double?), MethodExpressor(m => m.ConvertToNullableDouble (null)) },
|
|
257 { typeof(Single?), MethodExpressor(m => m.ConvertToNullableSingle (null)) },
|
|
258 { typeof(Boolean?), MethodExpressor(m => m.ConvertToNullableBoolean (null)) },
|
|
259 { typeof(DateTime?), MethodExpressor(m => m.ConvertToNullableDateTime (null)) },
|
|
260 { typeof(TimeSpan?), MethodExpressor(m => m.ConvertToNullableTimeSpan (null)) },
|
|
261 { typeof(DateTimeOffset?), MethodExpressor(m => m.ConvertToNullableDateTimeOffset(null)) },
|
|
262 { typeof(Decimal?), MethodExpressor(m => m.ConvertToNullableDecimal (null)) },
|
|
263 { typeof(Guid?), MethodExpressor(m => m.ConvertToNullableGuid (null)) },
|
|
264
|
|
265 #if !SILVERLIGHT
|
|
266
|
|
267 // SqlTypes
|
|
268 //
|
|
269 { typeof(SqlByte), MethodExpressor(m => m.ConvertToSqlByte (null)) },
|
|
270 { typeof(SqlInt16), MethodExpressor(m => m.ConvertToSqlInt16 (null)) },
|
|
271 { typeof(SqlInt32), MethodExpressor(m => m.ConvertToSqlInt32 (null)) },
|
|
272 { typeof(SqlInt64), MethodExpressor(m => m.ConvertToSqlInt64 (null)) },
|
|
273 { typeof(SqlSingle), MethodExpressor(m => m.ConvertToSqlSingle (null)) },
|
|
274 { typeof(SqlBoolean), MethodExpressor(m => m.ConvertToSqlBoolean (null)) },
|
|
275 { typeof(SqlDouble), MethodExpressor(m => m.ConvertToSqlDouble (null)) },
|
|
276 { typeof(SqlDateTime), MethodExpressor(m => m.ConvertToSqlDateTime (null)) },
|
|
277 { typeof(SqlDecimal), MethodExpressor(m => m.ConvertToSqlDecimal (null)) },
|
|
278 { typeof(SqlMoney), MethodExpressor(m => m.ConvertToSqlMoney (null)) },
|
|
279 { typeof(SqlString), MethodExpressor(m => m.ConvertToSqlString (null)) },
|
|
280 { typeof(SqlBinary), MethodExpressor(m => m.ConvertToSqlBinary (null)) },
|
|
281 { typeof(SqlGuid), MethodExpressor(m => m.ConvertToSqlGuid (null)) },
|
|
282 { typeof(SqlBytes), MethodExpressor(m => m.ConvertToSqlBytes (null)) },
|
|
283 { typeof(SqlChars), MethodExpressor(m => m.ConvertToSqlChars (null)) },
|
|
284 { typeof(SqlXml), MethodExpressor(m => m.ConvertToSqlXml (null)) },
|
|
285
|
|
286 #endif
|
|
287 };
|
|
288 }
|
|
289
|
|
290 public class Functions
|
|
291 {
|
|
292 public class String : Expressor<string>
|
|
293 {
|
|
294 //public static MethodInfo Contains = MethodExpressor(s => s.Contains(""));
|
|
295 //public static MethodInfo StartsWith = MethodExpressor(s => s.StartsWith(""));
|
|
296 //public static MethodInfo EndsWith = MethodExpressor(s => s.EndsWith(""));
|
|
297
|
|
298 #if !SILVERLIGHT
|
|
299 public static MethodInfo Like11 = MethodExpressor(s => System.Data.Linq.SqlClient.SqlMethods.Like("", ""));
|
|
300 public static MethodInfo Like12 = MethodExpressor(s => System.Data.Linq.SqlClient.SqlMethods.Like("", "", ' '));
|
|
301 #endif
|
|
302
|
|
303 public static MethodInfo Like21 = MethodExpressor(s => Sql.Like(s, ""));
|
|
304 public static MethodInfo Like22 = MethodExpressor(s => Sql.Like(s, "", ' '));
|
|
305 }
|
|
306 }
|
|
307 }
|
|
308 }
|