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