Expression.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. //
  2. // Expression.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // (C) 2008 Novell, Inc. (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Collections.ObjectModel;
  32. using System.Reflection;
  33. namespace System.Linq.Expressions {
  34. public abstract class Expression {
  35. ExpressionType node_type;
  36. Type type;
  37. public ExpressionType NodeType {
  38. get { return node_type; }
  39. }
  40. public Type Type {
  41. get { return type; }
  42. }
  43. // TODO: remove when all Expression subtypes
  44. // have their constructor implemented
  45. protected Expression ()
  46. {
  47. }
  48. protected Expression (ExpressionType node_type, Type type)
  49. {
  50. this.node_type = node_type;
  51. this.type = type;
  52. }
  53. public override string ToString ()
  54. {
  55. return ExpressionPrinter.ToString (this);
  56. }
  57. #region Binary Expressions
  58. static void BinaryCoreCheck (Expression left, Expression right)
  59. {
  60. if (left == null)
  61. throw new ArgumentNullException ("left");
  62. if (right == null)
  63. throw new ArgumentNullException ("right");
  64. if (left.Type != right.Type)
  65. throw new InvalidOperationException ("Both expressions must have the same type");
  66. }
  67. static BinaryExpression MakeSimpleBinary (ExpressionType et, Expression left, Expression right, MethodInfo method)
  68. {
  69. return new BinaryExpression (et, left.Type, left, right, method);
  70. }
  71. static BinaryExpression MakeSimpleBinary (ExpressionType et, Type result, Expression left, Expression right, MethodInfo method)
  72. {
  73. return new BinaryExpression (et, result, left, right, method);
  74. }
  75. static BinaryExpression MakeBoolBinary (ExpressionType et, Expression left, Expression right, bool liftToNull, MethodInfo method)
  76. {
  77. return new BinaryExpression (et, typeof (bool), left, right, method);
  78. }
  79. //
  80. // Arithmetic
  81. //
  82. public static BinaryExpression Add (Expression left, Expression right)
  83. {
  84. return Add (left, right, null);
  85. }
  86. public static BinaryExpression Add (Expression left, Expression right, MethodInfo method)
  87. {
  88. BinaryCoreCheck (left, right);
  89. return MakeSimpleBinary (ExpressionType.Add, left, right, method);
  90. }
  91. public static BinaryExpression AddChecked (Expression left, Expression right)
  92. {
  93. return AddChecked (left, right, null);
  94. }
  95. public static BinaryExpression AddChecked (Expression left, Expression right, MethodInfo method)
  96. {
  97. return MakeSimpleBinary (ExpressionType.AddChecked, left, right, method);
  98. }
  99. public static BinaryExpression Subtract (Expression left, Expression right)
  100. {
  101. return Subtract (left, right, null);
  102. }
  103. public static BinaryExpression Subtract (Expression left, Expression right, MethodInfo method)
  104. {
  105. BinaryCoreCheck (left, right);
  106. return MakeSimpleBinary (ExpressionType.Subtract, left, right, method);
  107. }
  108. public static BinaryExpression SubtractChecked (Expression left, Expression right)
  109. {
  110. return SubtractChecked (left, right, null);
  111. }
  112. public static BinaryExpression SubtractChecked (Expression left, Expression right, MethodInfo method)
  113. {
  114. BinaryCoreCheck (left, right);
  115. return MakeSimpleBinary (ExpressionType.SubtractChecked, left, right, method);
  116. }
  117. public static BinaryExpression Modulo (Expression left, Expression right)
  118. {
  119. return Modulo (left, right, null);
  120. }
  121. public static BinaryExpression Modulo (Expression left, Expression right, MethodInfo method)
  122. {
  123. BinaryCoreCheck (left, right);
  124. return MakeSimpleBinary (ExpressionType.Modulo, left, right, method);
  125. }
  126. public static BinaryExpression Multiply (Expression left, Expression right)
  127. {
  128. return Multiply (left, right, null);
  129. }
  130. public static BinaryExpression Multiply (Expression left, Expression right, MethodInfo method)
  131. {
  132. BinaryCoreCheck (left, right);
  133. return MakeSimpleBinary (ExpressionType.Multiply, left, right, method);
  134. }
  135. public static BinaryExpression MultiplyChecked (Expression left, Expression right)
  136. {
  137. return MultiplyChecked (left, right, null);
  138. }
  139. public static BinaryExpression MultiplyChecked (Expression left, Expression right, MethodInfo method)
  140. {
  141. BinaryCoreCheck (left, right);
  142. return MakeSimpleBinary (ExpressionType.MultiplyChecked, left, right, method);
  143. }
  144. public static BinaryExpression Divide (Expression left, Expression right)
  145. {
  146. return Divide (left, right, null);
  147. }
  148. public static BinaryExpression Divide (Expression left, Expression right, MethodInfo method)
  149. {
  150. BinaryCoreCheck (left, right);
  151. return MakeSimpleBinary (ExpressionType.Divide, left, right, method);
  152. }
  153. public static BinaryExpression Power (Expression left, Expression right)
  154. {
  155. return Power (left, right, null);
  156. }
  157. public static BinaryExpression Power (Expression left, Expression right, MethodInfo method)
  158. {
  159. BinaryCoreCheck (left, right);
  160. if (left.Type != typeof (double))
  161. throw new InvalidOperationException ("Power only supports double arguments");
  162. return MakeSimpleBinary (ExpressionType.Power, left, right, method);
  163. }
  164. //
  165. // Bitwise
  166. //
  167. static bool IsInt (Type t)
  168. {
  169. return
  170. t == typeof (int) || t == typeof (uint) ||
  171. t == typeof (short) || t == typeof (ushort) ||
  172. t == typeof (long) || t == typeof (ulong);
  173. }
  174. public static BinaryExpression And (Expression left, Expression right)
  175. {
  176. return And (left, right, null);
  177. }
  178. public static BinaryExpression And (Expression left, Expression right, MethodInfo method)
  179. {
  180. BinaryCoreCheck (left, right);
  181. if (left.Type == typeof (bool) || IsInt (left.Type))
  182. return MakeSimpleBinary (ExpressionType.And, left, right, method);
  183. throw new InvalidOperationException ("Only integral or bool types allowed");
  184. }
  185. public static BinaryExpression Or (Expression left, Expression right)
  186. {
  187. return Or (left, right, null);
  188. }
  189. public static BinaryExpression Or (Expression left, Expression right, MethodInfo method)
  190. {
  191. BinaryCoreCheck (left, right);
  192. if (left.Type == typeof (bool) || IsInt (left.Type))
  193. return MakeSimpleBinary (ExpressionType.Or, left, right, method);
  194. throw new InvalidOperationException ("Only integral or bool types allowed");
  195. }
  196. public static BinaryExpression ExclusiveOr (Expression left, Expression right)
  197. {
  198. return ExclusiveOr (left, right, null);
  199. }
  200. public static BinaryExpression ExclusiveOr (Expression left, Expression right, MethodInfo method)
  201. {
  202. BinaryCoreCheck (left, right);
  203. if (left.Type == typeof (bool) || IsInt (left.Type))
  204. return MakeSimpleBinary (ExpressionType.ExclusiveOr, left, right, method);
  205. throw new InvalidOperationException ("Only integral or bool types allowed");
  206. }
  207. public static BinaryExpression LeftShift (Expression left, Expression right)
  208. {
  209. return LeftShift (left, right, null);
  210. }
  211. public static BinaryExpression LeftShift (Expression left, Expression right, MethodInfo method)
  212. {
  213. BinaryCoreCheck (left, right);
  214. if (left.Type == typeof (int))
  215. return MakeSimpleBinary (ExpressionType.LeftShift, left, right, method);
  216. throw new InvalidOperationException ("Only int32 is allowed for shifts");
  217. }
  218. public static BinaryExpression RightShift (Expression left, Expression right)
  219. {
  220. return RightShift (left, right, null);
  221. }
  222. public static BinaryExpression RightShift (Expression left, Expression right, MethodInfo method)
  223. {
  224. BinaryCoreCheck (left, right);
  225. if (left.Type == typeof (int))
  226. return MakeSimpleBinary (ExpressionType.RightShift, left, right, method);
  227. throw new InvalidOperationException ("Only int32 is allowed for shifts");
  228. }
  229. //
  230. // Short-circuit
  231. //
  232. public static BinaryExpression AndAlso (Expression left, Expression right)
  233. {
  234. return AndAlso (left, right, null);
  235. }
  236. [MonoTODO]
  237. public static BinaryExpression AndAlso (Expression left, Expression right, MethodInfo method)
  238. {
  239. // This does not work with int, int pairs; Figure out when its valid
  240. throw new NotImplementedException ();
  241. }
  242. [MonoTODO]
  243. public static BinaryExpression OrElse (Expression left, Expression right)
  244. {
  245. throw new NotImplementedException ();
  246. }
  247. [MonoTODO]
  248. public static BinaryExpression OrElse (Expression left, Expression right, MethodInfo method)
  249. {
  250. throw new NotImplementedException ();
  251. }
  252. //
  253. // Comparison
  254. //
  255. public static BinaryExpression Equal (Expression left, Expression right)
  256. {
  257. return Equal (left, right, false, null);
  258. }
  259. public static BinaryExpression Equal (Expression left, Expression right, bool liftToNull, MethodInfo method)
  260. {
  261. BinaryCoreCheck (left, right);
  262. return MakeBoolBinary (ExpressionType.Equal, left, right, liftToNull, method);
  263. }
  264. public static BinaryExpression NotEqual (Expression left, Expression right)
  265. {
  266. return NotEqual (left, right, false, null);
  267. }
  268. public static BinaryExpression NotEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  269. {
  270. BinaryCoreCheck (left, right);
  271. return MakeBoolBinary (ExpressionType.NotEqual, left, right, liftToNull, method);
  272. }
  273. public static BinaryExpression GreaterThan (Expression left, Expression right)
  274. {
  275. return GreaterThan (left, right, false, null);
  276. }
  277. public static BinaryExpression GreaterThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  278. {
  279. BinaryCoreCheck (left, right);
  280. return MakeBoolBinary (ExpressionType.GreaterThan, left, right, liftToNull, method);
  281. }
  282. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right)
  283. {
  284. return GreaterThanOrEqual (left, right, false, null);
  285. }
  286. public static BinaryExpression GreaterThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  287. {
  288. BinaryCoreCheck (left, right);
  289. return MakeBoolBinary (ExpressionType.GreaterThanOrEqual, left, right, liftToNull, method);
  290. }
  291. public static BinaryExpression LessThan (Expression left, Expression right)
  292. {
  293. return LessThan (left, right, false, null);
  294. }
  295. public static BinaryExpression LessThan (Expression left, Expression right, bool liftToNull, MethodInfo method)
  296. {
  297. return MakeBoolBinary (ExpressionType.LessThan, left, right, liftToNull, method);
  298. }
  299. public static BinaryExpression LessThanOrEqual (Expression left, Expression right)
  300. {
  301. return LessThanOrEqual (left, right, false, null);
  302. }
  303. public static BinaryExpression LessThanOrEqual (Expression left, Expression right, bool liftToNull, MethodInfo method)
  304. {
  305. BinaryCoreCheck (left, right);
  306. return MakeBoolBinary (ExpressionType.LessThanOrEqual, left, right, liftToNull, method);
  307. }
  308. //
  309. // Miscelaneous
  310. //
  311. [MonoTODO]
  312. public static BinaryExpression ArrayIndex (Expression left, Expression index)
  313. {
  314. throw new NotImplementedException ();
  315. }
  316. public static BinaryExpression Coalesce (Expression left, Expression right)
  317. {
  318. return Coalesce (left, right, null);
  319. }
  320. [MonoTODO]
  321. public static BinaryExpression Coalesce (Expression left, Expression right, LambdaExpression conversion)
  322. {
  323. BinaryCoreCheck (left, right);
  324. throw new NotImplementedException ();
  325. }
  326. //
  327. // MakeBinary constructors
  328. //
  329. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right)
  330. {
  331. return MakeBinary (binaryType, left, right, false, null);
  332. }
  333. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method)
  334. {
  335. return MakeBinary (binaryType, left, right, liftToNull, method, null);
  336. }
  337. public static BinaryExpression MakeBinary (ExpressionType binaryType, Expression left, Expression right, bool liftToNull, MethodInfo method, LambdaExpression conversion)
  338. {
  339. switch (binaryType) {
  340. case ExpressionType.Add:
  341. return Add (left, right, method);
  342. case ExpressionType.AddChecked:
  343. return AddChecked (left, right, method);
  344. case ExpressionType.AndAlso:
  345. return AndAlso (left, right);
  346. case ExpressionType.Coalesce:
  347. return Coalesce (left, right, conversion);
  348. case ExpressionType.Divide:
  349. return Divide (left, right, method);
  350. case ExpressionType.Equal:
  351. return Equal (left, right, liftToNull, method);
  352. case ExpressionType.ExclusiveOr:
  353. return ExclusiveOr (left, right, method);
  354. case ExpressionType.GreaterThan:
  355. return GreaterThan (left, right, liftToNull, method);
  356. case ExpressionType.GreaterThanOrEqual:
  357. return GreaterThanOrEqual (left, right, liftToNull, method);
  358. case ExpressionType.LeftShift:
  359. return LeftShift (left, right, method);
  360. case ExpressionType.LessThan:
  361. return LessThan (left, right, liftToNull, method);
  362. case ExpressionType.LessThanOrEqual:
  363. return LessThanOrEqual (left, right, liftToNull, method);
  364. case ExpressionType.Modulo:
  365. return Modulo (left, right, method);
  366. case ExpressionType.Multiply:
  367. return Multiply (left, right, method);
  368. case ExpressionType.MultiplyChecked:
  369. return MultiplyChecked (left, right, method);
  370. case ExpressionType.NotEqual:
  371. return NotEqual (left, right, liftToNull, method);
  372. case ExpressionType.OrElse:
  373. return OrElse (left, right);
  374. case ExpressionType.Power:
  375. return Power (left, right, method);
  376. case ExpressionType.RightShift:
  377. return RightShift (left, right, method);
  378. case ExpressionType.Subtract:
  379. return Subtract (left, right, method);
  380. case ExpressionType.SubtractChecked:
  381. return SubtractChecked (left, right, method);
  382. case ExpressionType.And:
  383. return And (left, right, method);
  384. case ExpressionType.Or:
  385. return Or (left, right, method);
  386. }
  387. throw new ArgumentException ("MakeBinary expect a binary node type");
  388. }
  389. #endregion
  390. [MonoTODO]
  391. public static MethodCallExpression ArrayIndex (Expression left, params Expression [] indexes)
  392. {
  393. throw new NotImplementedException ();
  394. }
  395. [MonoTODO]
  396. public static MethodCallExpression ArrayIndex (Expression left, IEnumerable<Expression> indexes)
  397. {
  398. throw new NotImplementedException ();
  399. }
  400. public static UnaryExpression ArrayLength (Expression array)
  401. {
  402. if (array == null)
  403. throw new ArgumentNullException ("array");
  404. if (!array.Type.IsArray)
  405. throw new ArgumentException ("The type of the expression must me Array");
  406. if (array.Type.GetArrayRank () != 1)
  407. throw new ArgumentException ("The array must be a single dimensional array");
  408. return new UnaryExpression (ExpressionType.ArrayLength, array, typeof (int));
  409. }
  410. [MonoTODO]
  411. public static MemberAssignment Bind (MemberInfo member, Expression expression)
  412. {
  413. throw new NotImplementedException ();
  414. }
  415. [MonoTODO]
  416. public static MemberAssignment Bind (MethodInfo propertyAccessor, Expression expression)
  417. {
  418. throw new NotImplementedException ();
  419. }
  420. [MonoTODO]
  421. public static MethodCallExpression Call (Expression instance, MethodInfo method)
  422. {
  423. throw new NotImplementedException ();
  424. }
  425. [MonoTODO]
  426. public static MethodCallExpression Call (MethodInfo method, params Expression [] arguments)
  427. {
  428. throw new NotImplementedException ();
  429. }
  430. [MonoTODO]
  431. public static MethodCallExpression Call (Expression instance, MethodInfo method, params Expression [] arguments)
  432. {
  433. throw new NotImplementedException ();
  434. }
  435. [MonoTODO]
  436. public static MethodCallExpression Call (Expression instance, MethodInfo method, IEnumerable<Expression> arguments)
  437. {
  438. throw new NotImplementedException ();
  439. }
  440. [MonoTODO]
  441. public static MethodCallExpression Call (Expression instance, string methodName, Type [] typeArguments, params Expression [] arguments)
  442. {
  443. throw new NotImplementedException ();
  444. }
  445. [MonoTODO]
  446. public static MethodCallExpression Call (Type type, string methodName, Type [] typeArguments, params Expression [] arguments)
  447. {
  448. throw new NotImplementedException ();
  449. }
  450. public static ConditionalExpression Condition (Expression test, Expression ifTrue, Expression ifFalse)
  451. {
  452. if (test == null)
  453. throw new ArgumentNullException ("test");
  454. if (ifTrue == null)
  455. throw new ArgumentNullException ("ifTrue");
  456. if (ifFalse == null)
  457. throw new ArgumentNullException ("ifFalse");
  458. if (test.Type != typeof (bool))
  459. throw new ArgumentException ("Test expression should be of type bool");
  460. if (ifTrue.Type != ifFalse.Type)
  461. throw new ArgumentException ("The ifTrue and ifFalse type do not match");
  462. return new ConditionalExpression (test, ifTrue, ifFalse);
  463. }
  464. public static ConstantExpression Constant (object value)
  465. {
  466. if (value == null)
  467. return new ConstantExpression (null, typeof (object));
  468. return Constant (value, value.GetType ());
  469. }
  470. public static ConstantExpression Constant (object value, Type type)
  471. {
  472. if (type == null)
  473. throw new ArgumentNullException ("type");
  474. //
  475. // value must be compatible with type, no conversions
  476. // are allowed
  477. //
  478. if (value == null){
  479. if (type.IsValueType && !IsNullable (type))
  480. throw new ArgumentException ();
  481. } else {
  482. if (!(type.IsValueType && IsNullable (type)) && value.GetType () != type)
  483. throw new ArgumentException ();
  484. }
  485. return new ConstantExpression (value, type);
  486. }
  487. [MonoTODO]
  488. public static UnaryExpression Convert (Expression expression, Type type)
  489. {
  490. throw new NotImplementedException ();
  491. }
  492. [MonoTODO]
  493. public static UnaryExpression Convert (Expression expression, Type type, MethodInfo method)
  494. {
  495. throw new NotImplementedException ();
  496. }
  497. [MonoTODO]
  498. public static UnaryExpression ConvertChecked (Expression expression, Type type)
  499. {
  500. throw new NotImplementedException ();
  501. }
  502. [MonoTODO]
  503. public static UnaryExpression ConvertChecked (Expression expression, Type type, MethodInfo method)
  504. {
  505. throw new NotImplementedException ();
  506. }
  507. [MonoTODO]
  508. public static ElementInit ElementInit (MethodInfo addMethod, params Expression [] arguments)
  509. {
  510. throw new NotImplementedException ();
  511. }
  512. [MonoTODO]
  513. public static ElementInit ElementInit (MethodInfo addMethod, IEnumerable<Expression> arguments)
  514. {
  515. throw new NotImplementedException ();
  516. }
  517. [MonoTODO]
  518. public static MemberExpression Field (Expression expression, FieldInfo field)
  519. {
  520. throw new NotImplementedException ();
  521. }
  522. [MonoTODO]
  523. public static MemberExpression Field (Expression expression, string fieldName)
  524. {
  525. throw new NotImplementedException ();
  526. }
  527. public static Type GetActionType (params Type [] typeArgs)
  528. {
  529. if (typeArgs == null)
  530. throw new ArgumentNullException ("typeArgs");
  531. if (typeArgs.Length > 4)
  532. throw new ArgumentException ("No Action type of this arity");
  533. if (typeArgs.Length == 0)
  534. return typeof (Action);
  535. Type action = null;
  536. switch (typeArgs.Length) {
  537. case 1:
  538. action = typeof (Action<>);
  539. break;
  540. case 2:
  541. action = typeof (Action<,>);
  542. break;
  543. case 3:
  544. action = typeof (Action<,,>);
  545. break;
  546. case 4:
  547. action = typeof (Action<,,,>);
  548. break;
  549. }
  550. return action.MakeGenericType (typeArgs);
  551. }
  552. public static Type GetFuncType (params Type [] typeArgs)
  553. {
  554. if (typeArgs == null)
  555. throw new ArgumentNullException ("typeArgs");
  556. if (typeArgs.Length < 1 || typeArgs.Length > 5)
  557. throw new ArgumentException ("No Func type of this arity");
  558. Type func = null;
  559. switch (typeArgs.Length) {
  560. case 1:
  561. func = typeof (Func<>);
  562. break;
  563. case 2:
  564. func = typeof (Func<,>);
  565. break;
  566. case 3:
  567. func = typeof (Func<,,>);
  568. break;
  569. case 4:
  570. func = typeof (Func<,,,>);
  571. break;
  572. case 5:
  573. func = typeof (Func<,,,,>);
  574. break;
  575. }
  576. return func.MakeGenericType (typeArgs);
  577. }
  578. [MonoTODO]
  579. public static InvocationExpression Invoke (Expression expression, params Expression [] arguments)
  580. {
  581. throw new NotImplementedException ();
  582. }
  583. [MonoTODO]
  584. public static InvocationExpression Invoke (Expression expression, IEnumerable<Expression> arguments)
  585. {
  586. throw new NotImplementedException ();
  587. }
  588. [MonoTODO]
  589. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, params ParameterExpression [] parameters)
  590. {
  591. throw new NotImplementedException ();
  592. }
  593. [MonoTODO]
  594. public static Expression<TDelegate> Lambda<TDelegate> (Expression body, IEnumerable<ParameterExpression> parameters)
  595. {
  596. throw new NotImplementedException ();
  597. }
  598. [MonoTODO]
  599. public static LambdaExpression Lambda (Expression body, params ParameterExpression [] parameters)
  600. {
  601. throw new NotImplementedException ();
  602. }
  603. [MonoTODO]
  604. public static LambdaExpression Lambda (Type delegateType, Expression body, params ParameterExpression [] parameters)
  605. {
  606. throw new NotImplementedException ();
  607. }
  608. [MonoTODO]
  609. public static LambdaExpression Lambda (Type delegateType, Expression body, IEnumerable<ParameterExpression> parameters)
  610. {
  611. throw new NotImplementedException ();
  612. }
  613. public static MemberListBinding ListBind (MemberInfo member, params ElementInit [] initializers)
  614. {
  615. throw new NotImplementedException ();
  616. }
  617. [MonoTODO]
  618. public static MemberListBinding ListBind (MemberInfo member, IEnumerable<ElementInit> initializers)
  619. {
  620. throw new NotImplementedException ();
  621. }
  622. [MonoTODO]
  623. public static MemberListBinding ListBind (MethodInfo propertyAccessor, params ElementInit [] initializers)
  624. {
  625. throw new NotImplementedException ();
  626. }
  627. [MonoTODO]
  628. public static MemberListBinding ListBind (MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers)
  629. {
  630. throw new NotImplementedException ();
  631. }
  632. [MonoTODO]
  633. public static ListInitExpression ListInit (NewExpression newExpression, params ElementInit [] initializers)
  634. {
  635. throw new NotImplementedException ();
  636. }
  637. [MonoTODO]
  638. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<ElementInit> initializers)
  639. {
  640. throw new NotImplementedException ();
  641. }
  642. [MonoTODO]
  643. public static ListInitExpression ListInit (NewExpression newExpression, params Expression [] initializers)
  644. {
  645. throw new NotImplementedException ();
  646. }
  647. [MonoTODO]
  648. public static ListInitExpression ListInit (NewExpression newExpression, IEnumerable<Expression> initializers)
  649. {
  650. throw new NotImplementedException ();
  651. }
  652. [MonoTODO]
  653. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, params Expression [] initializers)
  654. {
  655. throw new NotImplementedException ();
  656. }
  657. [MonoTODO]
  658. public static ListInitExpression ListInit (NewExpression newExpression, MethodInfo addMethod, IEnumerable<Expression> initializers)
  659. {
  660. throw new NotImplementedException ();
  661. }
  662. [MonoTODO]
  663. public static MemberExpression MakeMemberAccess (Expression expression, MemberInfo member)
  664. {
  665. throw new NotImplementedException ();
  666. }
  667. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type)
  668. {
  669. return MakeUnary (unaryType, operand, type, null);
  670. }
  671. public static UnaryExpression MakeUnary (ExpressionType unaryType, Expression operand, Type type, MethodInfo method)
  672. {
  673. switch (unaryType) {
  674. case ExpressionType.ArrayLength:
  675. return ArrayLength (operand);
  676. case ExpressionType.Convert:
  677. return Convert (operand, type, method);
  678. case ExpressionType.ConvertChecked:
  679. return ConvertChecked (operand, type, method);
  680. case ExpressionType.Negate:
  681. return Negate (operand, method);
  682. case ExpressionType.NegateChecked:
  683. return NegateChecked (operand, method);
  684. case ExpressionType.Not:
  685. return Not (operand, method);
  686. case ExpressionType.Quote:
  687. return Quote (operand);
  688. case ExpressionType.TypeAs:
  689. return TypeAs (operand, type);
  690. case ExpressionType.UnaryPlus:
  691. return UnaryPlus (operand, method);
  692. }
  693. throw new ArgumentException ("MakeUnary expect an unary operator");
  694. }
  695. [MonoTODO]
  696. public static MemberMemberBinding MemberBind (MemberInfo member, params MemberBinding [] binding)
  697. {
  698. throw new NotImplementedException ();
  699. }
  700. [MonoTODO]
  701. public static MemberMemberBinding MemberBind (MemberInfo member, IEnumerable<MemberBinding> binding)
  702. {
  703. throw new NotImplementedException ();
  704. }
  705. [MonoTODO]
  706. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, params MemberBinding [] binding)
  707. {
  708. throw new NotImplementedException ();
  709. }
  710. [MonoTODO]
  711. public static MemberMemberBinding MemberBind (MethodInfo propertyAccessor, IEnumerable<MemberBinding> binding)
  712. {
  713. throw new NotImplementedException ();
  714. }
  715. [MonoTODO]
  716. public static MemberInitExpression MemberInit (NewExpression newExpression, params MemberBinding [] binding)
  717. {
  718. throw new NotImplementedException ();
  719. }
  720. [MonoTODO]
  721. public static MemberInitExpression MemberInit (NewExpression newExpression, IEnumerable<MemberBinding> binding)
  722. {
  723. throw new NotImplementedException ();
  724. }
  725. [MonoTODO]
  726. public static UnaryExpression Negate (Expression expression)
  727. {
  728. throw new NotImplementedException ();
  729. }
  730. [MonoTODO]
  731. public static UnaryExpression Negate (Expression expression, MethodInfo method)
  732. {
  733. throw new NotImplementedException ();
  734. }
  735. [MonoTODO]
  736. public static UnaryExpression NegateChecked (Expression expression)
  737. {
  738. throw new NotImplementedException ();
  739. }
  740. [MonoTODO]
  741. public static UnaryExpression NegateChecked (Expression expression, MethodInfo method)
  742. {
  743. throw new NotImplementedException ();
  744. }
  745. [MonoTODO]
  746. public static NewExpression New (ConstructorInfo constructor)
  747. {
  748. throw new NotImplementedException ();
  749. }
  750. [MonoTODO]
  751. public static NewExpression New (Type type)
  752. {
  753. throw new NotImplementedException ();
  754. }
  755. [MonoTODO]
  756. public static NewExpression New (ConstructorInfo constructor, params Expression [] arguments)
  757. {
  758. throw new NotImplementedException ();
  759. }
  760. [MonoTODO]
  761. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments)
  762. {
  763. throw new NotImplementedException ();
  764. }
  765. [MonoTODO]
  766. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, params MemberInfo [] members)
  767. {
  768. throw new NotImplementedException ();
  769. }
  770. [MonoTODO]
  771. public static NewExpression New (ConstructorInfo constructor, IEnumerable<Expression> arguments, IEnumerable<MemberInfo> members)
  772. {
  773. throw new NotImplementedException ();
  774. }
  775. [MonoTODO]
  776. public static NewArrayExpression NewArrayBounds (Type type, params Expression [] bounds)
  777. {
  778. throw new NotImplementedException ();
  779. }
  780. [MonoTODO]
  781. public static NewArrayExpression NewArrayBounds (Type type, IEnumerable<Expression> bounds)
  782. {
  783. throw new NotImplementedException ();
  784. }
  785. [MonoTODO]
  786. public static NewArrayExpression NewArrayInit (Type type, params Expression [] bounds)
  787. {
  788. throw new NotImplementedException ();
  789. }
  790. [MonoTODO]
  791. public static NewArrayExpression NewArrayInit (Type type, IEnumerable<Expression> bounds)
  792. {
  793. throw new NotImplementedException ();
  794. }
  795. [MonoTODO]
  796. public static UnaryExpression Not (Expression expression)
  797. {
  798. throw new NotImplementedException ();
  799. }
  800. [MonoTODO]
  801. public static UnaryExpression Not (Expression expression, MethodInfo method)
  802. {
  803. throw new NotImplementedException ();
  804. }
  805. public static ParameterExpression Parameter (Type type, string name)
  806. {
  807. if (type == null)
  808. throw new ArgumentNullException ("type");
  809. return new ParameterExpression (type, name);
  810. }
  811. [MonoTODO]
  812. public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
  813. {
  814. throw new NotImplementedException ();
  815. }
  816. [MonoTODO]
  817. public static MemberExpression Property (Expression expression, PropertyInfo property)
  818. {
  819. throw new NotImplementedException ();
  820. }
  821. [MonoTODO]
  822. public static MemberExpression Property (Expression expression, string propertyName)
  823. {
  824. throw new NotImplementedException ();
  825. }
  826. [MonoTODO]
  827. public static MemberExpression PropertyOrField (Expression expression, string propertyOrFieldName)
  828. {
  829. throw new NotImplementedException ();
  830. }
  831. public static UnaryExpression Quote (Expression expression)
  832. {
  833. if (expression == null)
  834. throw new ArgumentNullException ("expression");
  835. return new UnaryExpression (ExpressionType.Quote, expression, expression.GetType ());
  836. }
  837. public static UnaryExpression TypeAs (Expression expression, Type type)
  838. {
  839. if (expression == null)
  840. throw new ArgumentNullException ("expression");
  841. if (type == null)
  842. throw new ArgumentNullException ("type");
  843. if (type.IsValueType && !IsNullable (type))
  844. throw new ArgumentException ("TypeAs expect a reference or a nullable type");
  845. return new UnaryExpression (ExpressionType.TypeAs, expression, type);
  846. }
  847. public static TypeBinaryExpression TypeIs (Expression expression, Type type)
  848. {
  849. if (expression == null)
  850. throw new ArgumentNullException ("expression");
  851. if (type == null)
  852. throw new ArgumentNullException ("type");
  853. return new TypeBinaryExpression (ExpressionType.TypeIs, expression, type, typeof (bool));
  854. }
  855. [MonoTODO]
  856. public static UnaryExpression UnaryPlus (Expression expression)
  857. {
  858. throw new NotImplementedException ();
  859. }
  860. [MonoTODO]
  861. public static UnaryExpression UnaryPlus (Expression expression, MethodInfo method)
  862. {
  863. throw new NotImplementedException ();
  864. }
  865. static bool IsNullable (Type type)
  866. {
  867. return type.IsGenericType && type.GetGenericTypeDefinition () == typeof (Nullable<>);
  868. }
  869. }
  870. }