PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Ast/MemberListBinding.cs

https://github.com/massimiliano-mantione/mono
C# | 141 lines | 69 code | 13 blank | 59 comment | 4 complexity | 274d55bd13cba3d3bf12f372e6f7dff6 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. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Collections.ObjectModel;
  19. using System.Dynamic.Utils;
  20. using System.Reflection;
  21. #if SILVERLIGHT
  22. using System.Core;
  23. #endif
  24. #if CLR2
  25. namespace Microsoft.Scripting.Ast {
  26. #else
  27. namespace System.Linq.Expressions {
  28. #endif
  29. /// <summary>
  30. /// Represents initializing the elements of a collection member of a newly created object.
  31. /// </summary>
  32. public sealed class MemberListBinding : MemberBinding {
  33. ReadOnlyCollection<ElementInit> _initializers;
  34. internal MemberListBinding(MemberInfo member, ReadOnlyCollection<ElementInit> initializers)
  35. #pragma warning disable 618
  36. : base(MemberBindingType.ListBinding, member) {
  37. #pragma warning restore 618
  38. _initializers = initializers;
  39. }
  40. /// <summary>
  41. /// Gets the element initializers for initializing a collection member of a newly created object.
  42. /// </summary>
  43. public ReadOnlyCollection<ElementInit> Initializers {
  44. get { return _initializers; }
  45. }
  46. /// <summary>
  47. /// Creates a new expression that is like this one, but using the
  48. /// supplied children. If all of the children are the same, it will
  49. /// return this expression.
  50. /// </summary>
  51. /// <param name="initializers">The <see cref="Initializers" /> property of the result.</param>
  52. /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
  53. public MemberListBinding Update(IEnumerable<ElementInit> initializers) {
  54. if (initializers == Initializers) {
  55. return this;
  56. }
  57. return Expression.ListBind(Member, initializers);
  58. }
  59. }
  60. public partial class Expression {
  61. ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> where the member is a field or property.</summary>
  62. ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" /> and the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> properties set to the specified values.</returns>
  63. ///<param name="member">A <see cref="T:System.Reflection.MemberInfo" /> that represents a field or property to set the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property equal to.</param>
  64. ///<param name="initializers">An array of <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
  65. ///<exception cref="T:System.ArgumentNullException">
  66. ///<paramref name="member" /> is null. -or-One or more elements of <paramref name="initializers" /> is null.</exception>
  67. ///<exception cref="T:System.ArgumentException">
  68. ///<paramref name="member" /> does not represent a field or property.-or-The <see cref="P:System.Reflection.FieldInfo.FieldType" /> or <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the field or property that <paramref name="member" /> represents does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
  69. public static MemberListBinding ListBind(MemberInfo member, params ElementInit[] initializers) {
  70. ContractUtils.RequiresNotNull(member, "member");
  71. ContractUtils.RequiresNotNull(initializers, "initializers");
  72. return ListBind(member, (IEnumerable<ElementInit>)initializers);
  73. }
  74. ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> where the member is a field or property.</summary>
  75. ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" /> and the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> properties set to the specified values.</returns>
  76. ///<param name="member">A <see cref="T:System.Reflection.MemberInfo" /> that represents a field or property to set the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property equal to.</param>
  77. ///<param name="initializers">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
  78. ///<exception cref="T:System.ArgumentNullException">
  79. ///<paramref name="member" /> is null. -or-One or more elements of <paramref name="initializers" /> is null.</exception>
  80. ///<exception cref="T:System.ArgumentException">
  81. ///<paramref name="member" /> does not represent a field or property.-or-The <see cref="P:System.Reflection.FieldInfo.FieldType" /> or <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the field or property that <paramref name="member" /> represents does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
  82. public static MemberListBinding ListBind(MemberInfo member, IEnumerable<ElementInit> initializers) {
  83. ContractUtils.RequiresNotNull(member, "member");
  84. ContractUtils.RequiresNotNull(initializers, "initializers");
  85. Type memberType;
  86. ValidateGettableFieldOrPropertyMember(member, out memberType);
  87. var initList = initializers.ToReadOnly();
  88. ValidateListInitArgs(memberType, initList);
  89. return new MemberListBinding(member, initList);
  90. }
  91. ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> object based on a specified property accessor method.</summary>
  92. ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" />, the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property set to the <see cref="T:System.Reflection.MemberInfo" /> that represents the property accessed in <paramref name="propertyAccessor" />, and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> populated with the elements of <paramref name="initializers" />.</returns>
  93. ///<param name="propertyAccessor">A <see cref="T:System.Reflection.MethodInfo" /> that represents a property accessor method.</param>
  94. ///<param name="initializers">An array of <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
  95. ///<exception cref="T:System.ArgumentNullException">
  96. ///<paramref name="propertyAccessor" /> is null. -or-One or more elements of <paramref name="initializers" /> is null.</exception>
  97. ///<exception cref="T:System.ArgumentException">
  98. ///<paramref name="propertyAccessor" /> does not represent a property accessor method.-or-The <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the property that the method represented by <paramref name="propertyAccessor" /> accesses does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
  99. public static MemberListBinding ListBind(MethodInfo propertyAccessor, params ElementInit[] initializers) {
  100. ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
  101. ContractUtils.RequiresNotNull(initializers, "initializers");
  102. return ListBind(propertyAccessor, (IEnumerable<ElementInit>)initializers);
  103. }
  104. ///<summary>Creates a <see cref="T:System.Linq.Expressions.MemberListBinding" /> based on a specified property accessor method.</summary>
  105. ///<returns>A <see cref="T:System.Linq.Expressions.MemberListBinding" /> that has the <see cref="P:System.Linq.Expressions.MemberBinding.BindingType" /> property equal to <see cref="F:System.Linq.Expressions.MemberBindingType.ListBinding" />, the <see cref="P:System.Linq.Expressions.MemberBinding.Member" /> property set to the <see cref="T:System.Reflection.MemberInfo" /> that represents the property accessed in <paramref name="propertyAccessor" />, and <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> populated with the elements of <paramref name="initializers" />.</returns>
  106. ///<param name="propertyAccessor">A <see cref="T:System.Reflection.MethodInfo" /> that represents a property accessor method.</param>
  107. ///<param name="initializers">An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains <see cref="T:System.Linq.Expressions.ElementInit" /> objects to use to populate the <see cref="P:System.Linq.Expressions.MemberListBinding.Initializers" /> collection.</param>
  108. ///<exception cref="T:System.ArgumentNullException">
  109. ///<paramref name="propertyAccessor" /> is null. -or-One or more elements of <paramref name="initializers" /> are null.</exception>
  110. ///<exception cref="T:System.ArgumentException">
  111. ///<paramref name="propertyAccessor" /> does not represent a property accessor method.-or-The <see cref="P:System.Reflection.PropertyInfo.PropertyType" /> of the property that the method represented by <paramref name="propertyAccessor" /> accesses does not implement <see cref="T:System.Collections.IEnumerable" />.</exception>
  112. public static MemberListBinding ListBind(MethodInfo propertyAccessor, IEnumerable<ElementInit> initializers) {
  113. ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
  114. ContractUtils.RequiresNotNull(initializers, "initializers");
  115. return ListBind(GetProperty(propertyAccessor), initializers);
  116. }
  117. private static void ValidateListInitArgs(Type listType, ReadOnlyCollection<ElementInit> initializers) {
  118. if (!typeof(IEnumerable).IsAssignableFrom(listType)) {
  119. throw Error.TypeNotIEnumerable(listType);
  120. }
  121. for (int i = 0, n = initializers.Count; i < n; i++) {
  122. ElementInit element = initializers[i];
  123. ContractUtils.RequiresNotNull(element, "initializers");
  124. ValidateCallInstanceType(listType, element.AddMethod);
  125. }
  126. }
  127. }
  128. }