comparison Source/Data/Linq/ReflectionHelper.cs @ 0:f990fcb411a9

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