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

/Microsoft.Scripting/Utils/DynamicUtils.cs

https://bitbucket.org/stefanrusek/xronos
C# | 60 lines | 29 code | 6 blank | 25 comment | 5 complexity | 97ce198fe1bbcd31a2120a3f731b08d1 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; using Microsoft;
  16. #if CODEPLEX_40
  17. using System.Dynamic;
  18. using System.Linq.Expressions;
  19. #else
  20. using Microsoft.Scripting;
  21. using Microsoft.Linq.Expressions;
  22. #endif
  23. namespace Microsoft.Scripting.Utils {
  24. public static class DynamicUtils {
  25. /// <summary>
  26. /// Returns the list of expressions represented by the <see cref="DynamicMetaObject"/> instances.
  27. /// </summary>
  28. /// <param name="objects">An array of <see cref="DynamicMetaObject"/> instances to extract expressions from.</param>
  29. /// <returns>The array of expressions.</returns>
  30. public static Expression[] GetExpressions(DynamicMetaObject[] objects) {
  31. ContractUtils.RequiresNotNull(objects, "objects");
  32. Expression[] res = new Expression[objects.Length];
  33. for (int i = 0; i < objects.Length; i++) {
  34. DynamicMetaObject mo = objects[i];
  35. res[i] = mo != null ? mo.Expression : null;
  36. }
  37. return res;
  38. }
  39. /// <summary>
  40. /// Creates an instance of <see cref="DynamicMetaObject"/> for a runtime value and the expression that represents it during the binding process.
  41. /// </summary>
  42. /// <param name="argValue">The runtime value to be represented by the <see cref="DynamicMetaObject"/>.</param>
  43. /// <param name="parameterExpression">An expression to represent this <see cref="DynamicMetaObject"/> during the binding process.</param>
  44. /// <returns>The new instance of <see cref="DynamicMetaObject"/>.</returns>
  45. public static DynamicMetaObject ObjectToMetaObject(object argValue, Expression parameterExpression) {
  46. IDynamicMetaObjectProvider ido = argValue as IDynamicMetaObjectProvider;
  47. if (ido != null) {
  48. return ido.GetMetaObject(parameterExpression);
  49. } else {
  50. return new DynamicMetaObject(parameterExpression, BindingRestrictions.Empty, argValue);
  51. }
  52. }
  53. }
  54. }