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

/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/ConvertBinder.cs

https://github.com/rasmus-toftdahl-olesen/mono
C# | 101 lines | 40 code | 12 blank | 49 comment | 3 complexity | 01191f5479604327aa424dbc822fa9ea 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.Dynamic.Utils;
  16. namespace System.Dynamic {
  17. /// <summary>
  18. /// Represents the convert dynamic operation at the call site, providing the binding semantic and the details about the operation.
  19. /// </summary>
  20. public abstract class ConvertBinder : DynamicMetaObjectBinder {
  21. private readonly Type _type;
  22. private readonly bool _explicit;
  23. /// <summary>
  24. /// Initializes a new intsance of the <see cref="ConvertBinder" />.
  25. /// </summary>
  26. /// <param name="type">The type to convert to.</param>
  27. /// <param name="explicit">true if the conversion should consider explicit conversions; otherwise, false.</param>
  28. protected ConvertBinder(Type type, bool @explicit) {
  29. ContractUtils.RequiresNotNull(type, "type");
  30. _type = type;
  31. _explicit = @explicit;
  32. }
  33. /// <summary>
  34. /// The type to convert to.
  35. /// </summary>
  36. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods")]
  37. public Type Type {
  38. get {
  39. return _type;
  40. }
  41. }
  42. /// <summary>
  43. /// Gets the value indicating if the conversion should consider explicit conversions.
  44. /// </summary>
  45. public bool Explicit {
  46. get {
  47. return _explicit;
  48. }
  49. }
  50. /// <summary>
  51. /// Performs the binding of the dynamic convert operation if the target dynamic object cannot bind.
  52. /// </summary>
  53. /// <param name="target">The target of the dynamic convert operation.</param>
  54. /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
  55. public DynamicMetaObject FallbackConvert(DynamicMetaObject target) {
  56. return FallbackConvert(target, null);
  57. }
  58. /// <summary>
  59. /// When overridden in the derived class, performs the binding of the dynamic convert operation if the target dynamic object cannot bind.
  60. /// </summary>
  61. /// <param name="target">The target of the dynamic convert operation.</param>
  62. /// <param name="errorSuggestion">The binding result to use if binding fails, or null.</param>
  63. /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
  64. public abstract DynamicMetaObject FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion);
  65. /// <summary>
  66. /// Performs the binding of the dynamic convert operation.
  67. /// </summary>
  68. /// <param name="target">The target of the dynamic convert operation.</param>
  69. /// <param name="args">An array of arguments of the dynamic convert operation.</param>
  70. /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
  71. public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
  72. ContractUtils.RequiresNotNull(target, "target");
  73. ContractUtils.Requires(args == null || args.Length == 0, "args");
  74. return target.BindConvert(this);
  75. }
  76. // this is a standard DynamicMetaObjectBinder
  77. internal override sealed bool IsStandardBinder {
  78. get {
  79. return true;
  80. }
  81. }
  82. /// <summary>
  83. /// The result type of the operation.
  84. /// </summary>
  85. public override sealed Type ReturnType {
  86. get { return _type; }
  87. }
  88. }
  89. }