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