PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting.Core/Ast/ElementInit.cs

https://bitbucket.org/stefanrusek/xronos
C# | 129 lines | 77 code | 13 blank | 39 comment | 5 complexity | 7c5d8742341c8ba8920211ef9e0e4307 MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if CODEPLEX_40
  16. using System;
  17. #else
  18. using System; using Microsoft;
  19. #endif
  20. using System.Collections.Generic;
  21. using System.Collections.ObjectModel;
  22. using System.Reflection;
  23. using System.Text;
  24. #if CODEPLEX_40
  25. using System.Dynamic.Utils;
  26. #else
  27. using Microsoft.Scripting.Utils;
  28. #endif
  29. #if CODEPLEX_40
  30. namespace System.Linq.Expressions {
  31. #else
  32. namespace Microsoft.Linq.Expressions {
  33. #endif
  34. /// <summary>
  35. /// Represents the initialization of a list.
  36. /// </summary>
  37. public sealed class ElementInit : IArgumentProvider {
  38. private MethodInfo _addMethod;
  39. private ReadOnlyCollection<Expression> _arguments;
  40. internal ElementInit(MethodInfo addMethod, ReadOnlyCollection<Expression> arguments) {
  41. _addMethod = addMethod;
  42. _arguments = arguments;
  43. }
  44. /// <summary>
  45. /// Gets the <see cref="MethodInfo"/> used to add elements to the object.
  46. /// </summary>
  47. public MethodInfo AddMethod {
  48. get { return _addMethod; }
  49. }
  50. /// <summary>
  51. /// Gets the list of elements to be added to the object.
  52. /// </summary>
  53. public ReadOnlyCollection<Expression> Arguments {
  54. get { return _arguments; }
  55. }
  56. Expression IArgumentProvider.GetArgument(int index) {
  57. return _arguments[index];
  58. }
  59. int IArgumentProvider.ArgumentCount {
  60. get {
  61. return _arguments.Count;
  62. }
  63. }
  64. /// <summary>
  65. /// Creates a <see cref="String"/> representation of the node.
  66. /// </summary>
  67. /// <returns>A <see cref="String"/> representation of the node.</returns>
  68. public override string ToString() {
  69. return ExpressionStringBuilder.ElementInitBindingToString(this);
  70. }
  71. }
  72. public partial class Expression {
  73. /// <summary>
  74. /// Creates an <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression that represents the initialization of a list.
  75. /// </summary>
  76. /// <param name="addMethod">The <see cref="MethodInfo"/> for the list's Add method.</param>
  77. /// <param name="arguments">An array containing the Expressions to be used to initialize the list.</param>
  78. /// <returns>The created <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression.</returns>
  79. public static ElementInit ElementInit(MethodInfo addMethod, params Expression[] arguments) {
  80. return ElementInit(addMethod, arguments as IEnumerable<Expression>);
  81. }
  82. /// <summary>
  83. /// Creates an <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression that represents the initialization of a list.
  84. /// </summary>
  85. /// <param name="addMethod">The <see cref="MethodInfo"/> for the list's Add method.</param>
  86. /// <param name="arguments">An <see cref="IEnumerable{T}"/> containing <see cref="Expression"/> elements to initialize the list.</param>
  87. /// <returns>The created <see cref="Microsoft.Linq.Expressions.ElementInit">ElementInit</see> expression.</returns>
  88. public static ElementInit ElementInit(MethodInfo addMethod, IEnumerable<Expression> arguments) {
  89. ContractUtils.RequiresNotNull(addMethod, "addMethod");
  90. ContractUtils.RequiresNotNull(arguments, "arguments");
  91. var argumentsRO = arguments.ToReadOnly();
  92. RequiresCanRead(argumentsRO, "arguments");
  93. ValidateElementInitAddMethodInfo(addMethod);
  94. ValidateArgumentTypes(addMethod, ExpressionType.Call, ref argumentsRO);
  95. return new ElementInit(addMethod, argumentsRO);
  96. }
  97. private static void ValidateElementInitAddMethodInfo(MethodInfo addMethod) {
  98. ValidateMethodInfo(addMethod);
  99. ParameterInfo[] pis = addMethod.GetParametersCached();
  100. if (pis.Length == 0) {
  101. throw Error.ElementInitializerMethodWithZeroArgs();
  102. }
  103. if (!addMethod.Name.Equals("Add", StringComparison.OrdinalIgnoreCase)) {
  104. throw Error.ElementInitializerMethodNotAdd();
  105. }
  106. if (addMethod.IsStatic) {
  107. throw Error.ElementInitializerMethodStatic();
  108. }
  109. foreach (ParameterInfo pi in pis) {
  110. if (pi.ParameterType.IsByRef) {
  111. throw Error.ElementInitializerMethodNoRefOutParam(pi.Name, addMethod.Name);
  112. }
  113. }
  114. }
  115. }
  116. }