DMSLinq表达式框架实现(二)
DMSExpressionVisitor类重写,其实这个类就是System.Linq下面的ExpressionVisitor类,改它也是因为更好控制我们的SQL语句而已.
下面贴出代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Linq.Expressions; 6 using System.Collections.ObjectModel; 7 8 namespace KingNET.DMSFrame.DMSLinq 9 { 10 public abstract class DMSExpressionVisitor 11 { 12 protected virtual Expression Visit(Expression exp) 13 { 14 if (exp == null) 15 { 16 return exp; 17 } 18 switch (exp.NodeType) 19 { 20 case ExpressionType.Add: 21 case ExpressionType.AddChecked: 22 case ExpressionType.And: 23 case ExpressionType.AndAlso: 24 case ExpressionType.ArrayIndex: 25 case ExpressionType.Coalesce: 26 case ExpressionType.Divide: 27 case ExpressionType.Equal: 28 case ExpressionType.ExclusiveOr: 29 case ExpressionType.GreaterThan: 30 case ExpressionType.GreaterThanOrEqual: 31 case ExpressionType.LeftShift: 32 case ExpressionType.LessThan: 33 case ExpressionType.LessThanOrEqual: 34 case ExpressionType.Modulo: 35 case ExpressionType.Multiply: 36 case ExpressionType.MultiplyChecked: 37 case ExpressionType.NotEqual: 38 case ExpressionType.Or: 39 case ExpressionType.OrElse: 40 case ExpressionType.RightShift: 41 case ExpressionType.Subtract: 42 case ExpressionType.SubtractChecked: 43 return this.VisitBinary((BinaryExpression)exp); 44 45 case ExpressionType.ArrayLength: 46 case ExpressionType.Convert: 47 case ExpressionType.ConvertChecked: 48 case ExpressionType.Negate: 49 case ExpressionType.NegateChecked: 50 case ExpressionType.Not: 51 case ExpressionType.Quote: 52 case ExpressionType.TypeAs: 53 return this.VisitUnary((UnaryExpression)exp); 54 55 case ExpressionType.Call: 56 return this.VisitMethodCall((MethodCallExpression)exp); 57 58 case ExpressionType.Conditional: 59 return this.VisitConditional((ConditionalExpression)exp); 60 61 case ExpressionType.Constant: 62 return this.VisitConstant((ConstantExpression)exp); 63 64 case ExpressionType.Invoke: 65 return this.VisitInvocation((InvocationExpression)exp); 66 67 case ExpressionType.Lambda: 68 return this.VisitLambda((LambdaExpression)exp); 69 70 case ExpressionType.ListInit: 71 return this.VisitListInit((ListInitExpression)exp); 72 73 case ExpressionType.MemberAccess: 74 return this.VisitMemberAccess((MemberExpression)exp); 75 76 case ExpressionType.MemberInit: 77 return this.VisitMemberInit((MemberInitExpression)exp); 78 79 case ExpressionType.New: 80 return this.VisitNew((NewExpression)exp); 81 82 case ExpressionType.NewArrayInit: 83 case ExpressionType.NewArrayBounds: 84 return this.VisitNewArray((NewArrayExpression)exp); 85 86 case ExpressionType.Parameter: 87 return this.VisitParameter((ParameterExpression)exp); 88 89 case ExpressionType.TypeIs: 90 return this.VisitTypeIs((TypeBinaryExpression)exp); 91 92 } 93 throw new Exception(string.Format("Unhandled expression type: '{0}'", exp.NodeType)); 94 } 95 protected virtual MemberBinding VisitBinding(MemberBinding binding) 96 { 97 switch (binding.BindingType) 98 { 99 case MemberBindingType.Assignment: 100 return this.VisitMemberAssignment((MemberAssignment)binding); 101 102 case MemberBindingType.MemberBinding: 103 return this.VisitMemberMemberBinding((MemberMemberBinding)binding); 104 105 case MemberBindingType.ListBinding: 106 return this.VisitMemberListBinding((MemberListBinding)binding); 107 108 default: 109 throw new Exception(string.Format("Unhandled binding type '{0}'", binding.BindingType)); 110 } 111 } 112 protected virtual ElementInit VisitElementInitializer(ElementInit initializer) 113 { 114 ReadOnlyCollection<Expression> readOnlyCollection = this.VisitExpressionList(initializer.Arguments); 115 if (readOnlyCollection != initializer.Arguments) 116 { 117 return Expression.ElementInit(initializer.AddMethod, readOnlyCollection); 118 } 119 return initializer; 120 } 121 protected virtual Expression VisitUnary(UnaryExpression u) 122 { 123 Expression expression = this.Visit(u.Operand); 124 if (expression != u.Operand) 125 { 126 return Expression.MakeUnary(u.NodeType, expression, u.Type, u.Method); 127 } 128 return u; 129 } 130 protected virtual Expression VisitBinary(BinaryExpression b) 131 { 132 Expression expression = this.Visit(b.Left); 133 Expression expression2 = this.Visit(b.Right); 134 Expression expression3 = this.Visit(b.Conversion); 135 if (expression == b.Left && expression2 == b.Right && expression3 == b.Conversion) 136 { 137 return b; 138 } 139 if (b.NodeType == ExpressionType.Coalesce && b.Conversion != null) 140 { 141 return Expression.Coalesce(expression, expression2, expression3 as LambdaExpression); 142 } 143 return Expression.MakeBinary(b.NodeType, expression, expression2, b.IsLiftedToNull, b.Method); 144 } 145 protected virtual Expression VisitTypeIs(TypeBinaryExpression b) 146 { 147 Expression expression = this.Visit(b.Expression); 148 if (expression != b.Expression) 149 { 150 return Expression.TypeIs(expression, b.TypeOperand); 151 } 152 return b; 153 } 154 protected virtual Expression VisitConstant(ConstantExpression c) 155 { 156 return c; 157 } 158 protected virtual Expression VisitConditional(ConditionalExpression c) 159 { 160 Expression expression = this.Visit(c.Test); 161 Expression expression2 = this.Visit(c.IfTrue); 162 Expression expression3 = this.Visit(c.IfFalse); 163 if (expression != c.Test || expression2 != c.IfTrue || expression3 != c.IfFalse) 164 { 165 return Expression.Condition(expression, expression2, expression3); 166 } 167 return c; 168 } 169 protected virtual Expression VisitParameter(ParameterExpression p) 170 { 171 return p; 172 } 173 protected virtual Expression VisitMemberAccess(MemberExpression m) 174 { 175 Expression expression = this.Visit(m.Expression); 176 if (expression != m.Expression) 177 { 178 return Expression.MakeMemberAccess(expression, m.Member); 179 } 180 return m; 181 } 182 protected virtual Expression VisitMethodCall(MethodCallExpression m) 183 { 184 Expression expression = this.Visit(m.Object); 185 IEnumerable<Expression> enumerable = this.VisitExpressionList(m.Arguments); 186 if (expression != m.Object || enumerable != m.Arguments) 187 { 188 return Expression.Call(expression, m.Method, enumerable); 189 } 190 return m; 191 } 192 protected virtual ReadOnlyCollection<Expression> VisitExpressionList(ReadOnlyCollection<Expression> original) 193 { 194 List<Expression> list = null; 195 int i = 0; 196 int count = original.Count; 197 while (i < count) 198 { 199 Expression expression = this.Visit(original[i]); 200 if (list != null) 201 { 202 list.Add(expression); 203 } 204 else 205 { 206 if (expression != original[i]) 207 { 208 list = new List<Expression>(count); 209 for (int j = 0; j < i; j++) 210 { 211 list.Add(original[j]); 212 } 213 list.Add(expression); 214 } 215 } 216 i++; 217 } 218 if (list != null) 219 { 220 return list.AsReadOnly(); 221 } 222 return original; 223 } 224 protected virtual MemberAssignment VisitMemberAssignment(MemberAssignment assignment) 225 { 226 Expression expression = this.Visit(assignment.Expression); 227 if (expression != assignment.Expression) 228 { 229 return Expression.Bind(assignment.Member, expression); 230 } 231 return assignment; 232 } 233 protected virtual MemberMemberBinding VisitMemberMemberBinding(MemberMemberBinding binding) 234 { 235 IEnumerable<MemberBinding> enumerable = this.VisitBindingList(binding.Bindings); 236 if (enumerable != binding.Bindings) 237 { 238 return Expression.MemberBind(binding.Member, enumerable); 239 } 240 return binding; 241 } 242 protected virtual MemberListBinding VisitMemberListBinding(MemberListBinding binding) 243 { 244 IEnumerable<ElementInit> enumerable = this.VisitElementInitializerList(binding.Initializers); 245 if (enumerable != binding.Initializers) 246 { 247 return Expression.ListBind(binding.Member, enumerable); 248 } 249 return binding; 250 } 251 protected virtual IEnumerable<MemberBinding> VisitBindingList(ReadOnlyCollection<MemberBinding> original) 252 { 253 List<MemberBinding> list = null; 254 int i = 0; 255 int count = original.Count; 256 while (i < count) 257 { 258 MemberBinding memberBinding = this.VisitBinding(original[i]); 259 if (list != null) 260 { 261 list.Add(memberBinding); 262 } 263 else 264 { 265 if (memberBinding != original[i]) 266 { 267 list = new List<MemberBinding>(count); 268 for (int j = 0; j < i; j++) 269 { 270 list.Add(original[j]); 271 } 272 list.Add(memberBinding); 273 } 274 } 275 i++; 276 } 277 if (list != null) 278 { 279 return list; 280 } 281 return original; 282 } 283 protected virtual IEnumerable<ElementInit> VisitElementInitializerList(ReadOnlyCollection<ElementInit> original) 284 { 285 List<ElementInit> list = null; 286 int i = 0; 287 int count = original.Count; 288 while (i < count) 289 { 290 ElementInit elementInit = this.VisitElementInitializer(original[i]); 291 if (list != null) 292 { 293 list.Add(elementInit); 294 } 295 else 296 { 297 if (elementInit != original[i]) 298 { 299 list = new List<ElementInit>(count); 300 for (int j = 0; j < i; j++) 301 { 302 list.Add(original[j]); 303 } 304 list.Add(elementInit); 305 } 306 } 307 i++; 308 } 309 if (list != null) 310 { 311 return list; 312 } 313 return original; 314 } 315 protected virtual Expression VisitLambda(LambdaExpression lambda) 316 { 317 Expression expression = this.Visit(lambda.Body); 318 if (expression != lambda.Body) 319 { 320 return Expression.Lambda(lambda.Type, expression, lambda.Parameters); 321 } 322 return lambda; 323 } 324 protected virtual NewExpression VisitNew(NewExpression nex) 325 { 326 IEnumerable<Expression> enumerable = this.VisitExpressionList(nex.Arguments); 327 if (enumerable == nex.Arguments) 328 { 329 return nex; 330 } 331 if (nex.Members != null) 332 { 333 return Expression.New(nex.Constructor, enumerable, nex.Members); 334 } 335 return Expression.New(nex.Constructor, enumerable); 336 } 337 protected virtual Expression VisitMemberInit(MemberInitExpression init) 338 { 339 NewExpression newExpression = this.VisitNew(init.NewExpression); 340 IEnumerable<MemberBinding> enumerable = this.VisitBindingList(init.Bindings); 341 if (newExpression != init.NewExpression || enumerable != init.Bindings) 342 { 343 return Expression.MemberInit(newExpression, enumerable); 344 } 345 return init; 346 } 347 protected virtual Expression VisitListInit(ListInitExpression init) 348 { 349 NewExpression newExpression = this.VisitNew(init.NewExpression); 350 IEnumerable<ElementInit> enumerable = this.VisitElementInitializerList(init.Initializers); 351 if (newExpression != init.NewExpression || enumerable != init.Initializers) 352 { 353 return Expression.ListInit(newExpression, enumerable); 354 } 355 return init; 356 } 357 protected virtual Expression VisitNewArray(NewArrayExpression na) 358 { 359 IEnumerable<Expression> enumerable = this.VisitExpressionList(na.Expressions); 360 if (enumerable == na.Expressions) 361 { 362 return na; 363 } 364 if (na.NodeType == ExpressionType.NewArrayInit) 365 { 366 return Expression.NewArrayInit(na.Type.GetElementType(), enumerable); 367 } 368 return Expression.NewArrayBounds(na.Type.GetElementType(), enumerable); 369 } 370 protected virtual Expression VisitInvocation(InvocationExpression iv) 371 { 372 IEnumerable<Expression> enumerable = this.VisitExpressionList(iv.Arguments); 373 Expression expression = this.Visit(iv.Expression); 374 if (enumerable != iv.Arguments || expression != iv.Expression) 375 { 376 return Expression.Invoke(expression, enumerable); 377 } 378 return iv; 379 } 380 } 381 }
相关项目下载可以联系本人QQ;
千人.NET交流群:18362376,因为有你,代码变得更简单,加群请输入cnblogs