Expression.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. //
  19. // Authors:
  20. // Marek Safar ([email protected])
  21. // Antonello Provenzano <[email protected]>
  22. //
  23. using System.Collections.Generic;
  24. using System.Collections.ObjectModel;
  25. using System.Reflection;
  26. using System.Text;
  27. namespace System.Linq.Expressions
  28. {
  29. public abstract class Expression
  30. {
  31. #region .ctor
  32. protected Expression(ExpressionType nodeType, Type type)
  33. {
  34. this.nodeType = nodeType;
  35. this.type = type;
  36. }
  37. #endregion
  38. #region Fields
  39. private Type type;
  40. private ExpressionType nodeType;
  41. #endregion
  42. #region Properties
  43. public Type Type
  44. {
  45. get { return type; }
  46. }
  47. public ExpressionType NodeType
  48. {
  49. get { return nodeType; }
  50. }
  51. #endregion
  52. #region Private Static Methods
  53. private static void CheckLeftRight(Expression left, Expression right)
  54. {
  55. if (left == null)
  56. throw new ArgumentNullException("left");
  57. if (right == null)
  58. throw new ArgumentNullException("right");
  59. }
  60. #endregion
  61. #region Internal Methods
  62. internal virtual void BuildString(StringBuilder builder)
  63. {
  64. //TODO:
  65. }
  66. #endregion
  67. #region Public Methods
  68. public override string ToString()
  69. {
  70. //TODO: check this...
  71. StringBuilder builder = new StringBuilder();
  72. BuildString(builder);
  73. return builder.ToString();
  74. }
  75. #endregion
  76. #region Internal Static Methos
  77. internal static Type GetNonNullableType(Type type)
  78. {
  79. //TODO:
  80. return type;
  81. }
  82. internal static bool IsNullableType(Type type)
  83. {
  84. if (type == null)
  85. throw new ArgumentNullException("type");
  86. if (type.IsGenericType)
  87. {
  88. //TODO: check this...
  89. Type genType = type.GetGenericTypeDefinition();
  90. return typeof(Nullable<>).IsAssignableFrom(genType);
  91. }
  92. return false;
  93. }
  94. #endregion
  95. #region Public Static Methods
  96. public static BinaryExpression Add(Expression left, Expression right, MethodInfo method)
  97. {
  98. CheckLeftRight(left, right);
  99. // sine both the expressions define the same numeric type we don't have
  100. // to look for the "op_Addition" method...
  101. if (left.type == right.type &&
  102. ExpressionUtil.IsNumber (left.type))
  103. return new BinaryExpression(ExpressionType.Add, left, right, left.type);
  104. if (method == null)
  105. {
  106. method = ExpressionUtil.GetMethod("op_Addition", new Type[] { left.type, right.type });
  107. }
  108. if (method != null)
  109. {
  110. if (method.ReturnType == null) // it returns a void
  111. throw new ArgumentException();
  112. if (!method.IsStatic)
  113. throw new ArgumentException();
  114. ParameterInfo[] pars = method.GetParameters();
  115. if (pars.Length != 2)
  116. throw new ArgumentException();
  117. }
  118. return new BinaryExpression(ExpressionType.Add, left, right, method, method.ReturnType);
  119. }
  120. public static BinaryExpression Add(Expression left, Expression right)
  121. {
  122. return Add(left, right, null);
  123. }
  124. public static BinaryExpression AddChecked(Expression left, Expression right, MethodInfo method)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. public static BinaryExpression AddChecked(Expression left, Expression right)
  129. {
  130. throw new NotImplementedException();
  131. }
  132. public static BinaryExpression And(Expression left, Expression right, MethodInfo method)
  133. {
  134. CheckLeftRight(left, right);
  135. throw new NotImplementedException();
  136. }
  137. public static BinaryExpression And(Expression left, Expression right)
  138. {
  139. return And(left, right, null);
  140. }
  141. public static BinaryExpression AndAlso(Expression left, Expression right, MethodInfo method)
  142. {
  143. throw new NotImplementedException();
  144. }
  145. public static BinaryExpression AndAlso(Expression left, Expression right)
  146. {
  147. return AndAlso(left, right, null);
  148. }
  149. public static MethodCallExpression ArrayIndex(Expression array, IEnumerable<Expression> indexes)
  150. {
  151. throw new NotImplementedException();
  152. }
  153. public static BinaryExpression ArrayIndex(Expression array, Expression index)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. public static MethodCallExpression ArrayIndex(Expression array, params Expression[] indexes)
  158. {
  159. throw new NotImplementedException();
  160. }
  161. public static UnaryExpression ArrayLength(Expression array)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. public static MemberAssignment Bind(MemberInfo member, Expression expression)
  166. {
  167. throw new NotImplementedException();
  168. }
  169. public static MemberAssignment Bind(MethodInfo propertyAccessor, Expression expression)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. public static MethodCallExpression Call(Expression instance, MethodInfo method)
  174. {
  175. throw new NotImplementedException();
  176. }
  177. public static MethodCallExpression Call(MethodInfo method, params Expression[] arguments)
  178. {
  179. throw new NotImplementedException();
  180. }
  181. public static MethodCallExpression Call(Expression instance, MethodInfo method, params Expression[] arguments)
  182. {
  183. throw new NotImplementedException();
  184. }
  185. public static MethodCallExpression Call(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
  186. {
  187. throw new NotImplementedException();
  188. }
  189. public static MethodCallExpression Call(Expression instance, string methodName, Type[] typeArguments, params Expression[] arguments)
  190. {
  191. throw new NotImplementedException();
  192. }
  193. public static MethodCallExpression Call(Type type, string methodName, Type[] typeArguments, params Expression[] arguments)
  194. {
  195. throw new NotImplementedException();
  196. }
  197. public static MethodCallExpression CallVirtual(Expression instance, MethodInfo method)
  198. {
  199. throw new NotImplementedException();
  200. }
  201. public static MethodCallExpression CallVirtual(Expression instance, MethodInfo method, params Expression[] arguments)
  202. {
  203. throw new NotImplementedException();
  204. }
  205. public static MethodCallExpression CallVirtual(Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
  206. {
  207. throw new NotImplementedException();
  208. }
  209. public static UnaryExpression Cast(Expression expression, Type type)
  210. {
  211. throw new NotImplementedException();
  212. }
  213. public static UnaryExpression Cast(Expression expression, Type type, MethodInfo method)
  214. {
  215. throw new NotImplementedException();
  216. }
  217. public static BinaryExpression Coalesce(Expression left, Expression right)
  218. {
  219. throw new NotImplementedException();
  220. }
  221. public static BinaryExpression Coalesce(Expression left, Expression right, MethodInfo conversion)
  222. {
  223. throw new NotImplementedException();
  224. }
  225. public static ConditionalExpression Condition(Expression test, Expression ifTrue, Expression ifFalse)
  226. {
  227. throw new NotImplementedException();
  228. }
  229. public static ConstantExpression Constant(object value)
  230. {
  231. Type valueType = null;
  232. if (value != null)
  233. valueType = value.GetType();
  234. return Constant(value, valueType);
  235. }
  236. public static ConstantExpression Constant(object value, Type type)
  237. {
  238. if (type == null)
  239. throw new ArgumentNullException("type");
  240. if (value == null && !IsNullableType(type))
  241. throw new ArgumentException();
  242. return new ConstantExpression(value, type);
  243. }
  244. public static UnaryExpression Convert(Expression expression, Type type)
  245. {
  246. throw new NotImplementedException();
  247. }
  248. public static UnaryExpression Convert(Expression expression, Type type, MethodInfo method)
  249. {
  250. throw new NotImplementedException();
  251. }
  252. public static UnaryExpression ConvertChecked(Expression expression, Type type)
  253. {
  254. throw new NotImplementedException();
  255. }
  256. public static UnaryExpression ConvertChecked(Expression expression, Type type, MethodInfo method)
  257. {
  258. throw new NotImplementedException();
  259. }
  260. public static BinaryExpression Divide(Expression left, Expression right)
  261. {
  262. return Divide(left, right);
  263. }
  264. public static BinaryExpression Divide(Expression left, Expression right, MethodInfo method)
  265. {
  266. CheckLeftRight(left, right);
  267. throw new NotImplementedException();
  268. }
  269. public static BinaryExpression Equal(Expression left, Expression right)
  270. {
  271. throw new NotImplementedException();
  272. }
  273. public static BinaryExpression Equal(Expression left, Expression right, bool liftToNull, MethodInfo method)
  274. {
  275. throw new NotImplementedException();
  276. }
  277. public static BinaryExpression ExclusiveOr(Expression left, Expression right)
  278. {
  279. throw new NotImplementedException();
  280. }
  281. public static BinaryExpression ExclusiveOr(Expression left, Expression right, MethodInfo method)
  282. {
  283. throw new NotImplementedException();
  284. }
  285. public static MemberExpression Field(Expression expression, FieldInfo field)
  286. {
  287. if (field == null)
  288. throw new ArgumentNullException("field");
  289. return new MemberExpression(expression, field, field.FieldType);
  290. }
  291. public static MemberExpression Field(Expression expression, string fieldName)
  292. {
  293. if (expression == null)
  294. throw new ArgumentNullException("expression");
  295. FieldInfo field = expression.Type.GetField(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  296. if (field == null)
  297. throw new ArgumentException();
  298. return Field(expression, field);
  299. }
  300. public static FuncletExpression Funclet(Funclet funclet, Type type)
  301. {
  302. if (funclet == null)
  303. throw new ArgumentNullException("funclet");
  304. if (type == null)
  305. throw new ArgumentNullException("type");
  306. return new FuncletExpression(funclet, type);
  307. }
  308. public static Type GetFuncType(params Type[] typeArgs)
  309. {
  310. if (typeArgs == null)
  311. throw new ArgumentNullException("typeArgs");
  312. if (typeArgs.Length > 5)
  313. throw new ArgumentException();
  314. return typeof(Func<,,,,>).MakeGenericType(typeArgs);
  315. }
  316. public static BinaryExpression GreaterThan(Expression left, Expression right)
  317. {
  318. throw new NotImplementedException();
  319. }
  320. public static BinaryExpression GreaterThan(Expression left, Expression right, bool liftToNull, MethodInfo method)
  321. {
  322. throw new NotImplementedException();
  323. }
  324. public static BinaryExpression GreaterThanOrEqual(Expression left, Expression right)
  325. {
  326. throw new NotImplementedException();
  327. }
  328. public static BinaryExpression GreaterThanOrEqual(Expression left, Expression right, bool liftToNull, MethodInfo method)
  329. {
  330. throw new NotImplementedException();
  331. }
  332. public static InvocationExpression Invoke(Expression expression, IEnumerable<Expression> arguments)
  333. {
  334. throw new NotImplementedException();
  335. }
  336. public static InvocationExpression Invoke(Expression expression, params Expression[] arguments)
  337. {
  338. throw new NotImplementedException();
  339. }
  340. public static Expression<TDelegate> Lambda<TDelegate>(Expression body, params ParameterExpression[] parameters)
  341. {
  342. throw new NotImplementedException();
  343. }
  344. public static Expression<TDelegate> Lambda<TDelegate>(Expression body, IEnumerable<ParameterExpression> parameters)
  345. {
  346. throw new NotImplementedException();
  347. }
  348. public static LambdaExpression Lambda(Expression body, params ParameterExpression[] parameters)
  349. {
  350. throw new NotImplementedException();
  351. }
  352. public static LambdaExpression Lambda(Type delegateType, Expression body, params ParameterExpression[] parameters)
  353. {
  354. throw new NotImplementedException();
  355. }
  356. public static LambdaExpression Lambda(Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
  357. {
  358. throw new NotImplementedException();
  359. }
  360. public static BinaryExpression LeftShift(Expression left, Expression right)
  361. {
  362. throw new NotImplementedException();
  363. }
  364. public static BinaryExpression LeftShift(Expression left, Expression right, MethodInfo method)
  365. {
  366. throw new NotImplementedException();
  367. }
  368. public static BinaryExpression LessThan(Expression left, Expression right)
  369. {
  370. throw new NotImplementedException();
  371. }
  372. public static BinaryExpression LessThan(Expression left, Expression right, bool liftToNull, MethodInfo method)
  373. {
  374. throw new NotImplementedException();
  375. }
  376. public static BinaryExpression LessThanOrEqual(Expression left, Expression right)
  377. {
  378. throw new NotImplementedException();
  379. }
  380. public static BinaryExpression LessThanOrEqual(Expression left, Expression right, bool liftToNull, MethodInfo method)
  381. {
  382. throw new NotImplementedException();
  383. }
  384. /*
  385. public static LiftExpression Lift(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
  386. {
  387. throw new NotImplementedException();
  388. }
  389. public static LiftExpression Lift(Expression expression, ParameterExpression parameter, Expression argument)
  390. {
  391. throw new NotImplementedException();
  392. }
  393. public static LiftExpression LiftEqual(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
  394. {
  395. throw new NotImplementedException();
  396. }
  397. public static LiftExpression LiftFalse(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
  398. {
  399. throw new NotImplementedException();
  400. }
  401. public static LiftExpression LiftFalse(Expression expression, ParameterExpression parameter, Expression argument)
  402. {
  403. throw new NotImplementedException();
  404. }
  405. public static LiftExpression LiftNotEqual(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
  406. {
  407. throw new NotImplementedException();
  408. }
  409. public static LiftExpression LiftTrue(Expression expression, IEnumerable<ParameterExpression> parameters, IEnumerable<Expression> arguments)
  410. {
  411. throw new NotImplementedException();
  412. }
  413. public static LiftExpression LiftTrue(Expression expression, ParameterExpression parameter, Expression argument)
  414. {
  415. throw new NotImplementedException();
  416. }
  417. */
  418. public static MemberListBinding ListBind(MemberInfo member, params Expression[] initializers)
  419. {
  420. throw new NotImplementedException();
  421. }
  422. public static MemberListBinding ListBind(MemberInfo member, IEnumerable<Expression> initializers)
  423. {
  424. throw new NotImplementedException();
  425. }
  426. public static MemberListBinding ListBind(MethodInfo propertyAccessor, IEnumerable<Expression> initializers)
  427. {
  428. throw new NotImplementedException();
  429. }
  430. public static ListInitExpression ListInit(NewExpression newExpression, params Expression[] initializers)
  431. {
  432. if (initializers == null)
  433. throw new ArgumentNullException("inizializers");
  434. return ListInit(newExpression, ExpressionUtil.GetReadOnlyCollection (initializers));
  435. }
  436. public static ListInitExpression ListInit(NewExpression newExpression, IEnumerable<Expression> initializers)
  437. {
  438. if (newExpression == null)
  439. throw new ArgumentNullException("newExpression");
  440. if (initializers == null)
  441. throw new ArgumentNullException("inizializers");
  442. return new ListInitExpression(newExpression, ExpressionUtil.GetReadOnlyCollection(initializers));
  443. }
  444. public static MemberInitExpression MemberInit(NewExpression newExpression, IEnumerable<MemberBinding> bindings)
  445. {
  446. if (newExpression == null)
  447. throw new ArgumentNullException("newExpression");
  448. if (bindings == null)
  449. throw new ArgumentNullException("bindings");
  450. return new MemberInitExpression(newExpression, ExpressionUtil.GetReadOnlyCollection(bindings));
  451. }
  452. public static MemberExpression Property(Expression expression, PropertyInfo property)
  453. {
  454. if (property == null)
  455. throw new ArgumentNullException("property");
  456. MethodInfo getMethod = property.GetGetMethod(true);
  457. if (getMethod == null)
  458. throw new ArgumentException(); // to access the property we need to have
  459. // a get method...
  460. return new MemberExpression(expression, property, property.PropertyType);
  461. }
  462. public static MemberExpression Property(Expression expression, string propertyName)
  463. {
  464. if (expression == null)
  465. throw new ArgumentNullException("expression");
  466. PropertyInfo property = expression.Type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  467. if (property == null)
  468. throw new ArgumentException();
  469. return Property(expression, property);
  470. }
  471. public static MemberExpression PropertyOrField(Expression expression, string propertyOrFieldName)
  472. {
  473. if (expression == null)
  474. throw new ArgumentNullException("expression");
  475. PropertyInfo property = expression.Type.GetProperty(propertyOrFieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  476. if (property != null)
  477. return Property(expression, property);
  478. FieldInfo field = expression.Type.GetField(propertyOrFieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
  479. if (field != null)
  480. return Field(expression, field);
  481. //TODO: should we return <null> here?
  482. // the name is not defined in the Type of the expression given...
  483. throw new ArgumentException();
  484. }
  485. public static UnaryExpression Quote(Expression expression)
  486. {
  487. if (expression == null)
  488. throw new ArgumentNullException("expression");
  489. //TODO: is this right?
  490. return new UnaryExpression(ExpressionType.Quote, expression, expression.GetType());
  491. }
  492. public static TypeBinaryExpression TypeIs(Expression expression, Type type)
  493. {
  494. if (expression == null)
  495. throw new ArgumentNullException("expression");
  496. if (type == null)
  497. throw new ArgumentNullException("type");
  498. return new TypeBinaryExpression(ExpressionType.TypeIs, expression, type, typeof(bool));
  499. }
  500. public static UnaryExpression TypeAs(Expression expression, Type type)
  501. {
  502. if (expression == null)
  503. throw new ArgumentNullException("expression");
  504. if (type == null)
  505. throw new ArgumentNullException("type");
  506. return new UnaryExpression(ExpressionType.TypeAs, expression, type);
  507. }
  508. #endregion
  509. }
  510. }