1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Dynamic.Utils;
6 
7 namespace System.Dynamic
8 {
9     /// <summary>
10     /// Represents the dynamic set member operation at the call site, providing the binding semantic and the details about the operation.
11     /// </summary>
12     public abstract class SetMemberBinder : DynamicMetaObjectBinder
13     {
14         /// <summary>
15         /// Initializes a new instance of the <see cref="SetMemberBinder" />.
16         /// </summary>
17         /// <param name="name">The name of the member to get.</param>
18         /// <param name="ignoreCase">true if the name should be matched ignoring case; false otherwise.</param>
SetMemberBinder(string name, bool ignoreCase)19         protected SetMemberBinder(string name, bool ignoreCase)
20         {
21             ContractUtils.RequiresNotNull(name, nameof(name));
22 
23             Name = name;
24             IgnoreCase = ignoreCase;
25         }
26 
27         /// <summary>
28         /// The result type of the operation.
29         /// </summary>
30         public override sealed Type ReturnType => typeof(object);
31 
32         /// <summary>
33         /// Gets the name of the member to get.
34         /// </summary>
35         public string Name { get; }
36 
37         /// <summary>
38         /// Gets the value indicating if the string comparison should ignore the case of the member name.
39         /// </summary>
40         public bool IgnoreCase { get; }
41 
42         /// <summary>
43         /// Performs the binding of the dynamic set member operation.
44         /// </summary>
45         /// <param name="target">The target of the dynamic set member operation.</param>
46         /// <param name="args">An array of arguments of the dynamic set member operation.</param>
47         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
Bind(DynamicMetaObject target, DynamicMetaObject[] args)48         public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
49         {
50             ContractUtils.RequiresNotNull(target, nameof(target));
51             ContractUtils.RequiresNotNull(args, nameof(args));
52             ContractUtils.Requires(args.Length == 1, nameof(args));
53 
54             var arg0 = args[0];
55             ContractUtils.RequiresNotNull(arg0, nameof(args));
56 
57             return target.BindSetMember(this, arg0);
58         }
59 
60         /// <summary>
61         /// Always returns <c>true</c> because this is a standard <see cref="DynamicMetaObjectBinder"/>.
62         /// </summary>
63         internal override sealed bool IsStandardBinder => true;
64 
65         /// <summary>
66         /// Performs the binding of the dynamic set member operation if the target dynamic object cannot bind.
67         /// </summary>
68         /// <param name="target">The target of the dynamic set member operation.</param>
69         /// <param name="value">The value to set to the member.</param>
70         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value)71         public DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value)
72         {
73             return FallbackSetMember(target, value, null);
74         }
75 
76         /// <summary>
77         /// Performs the binding of the dynamic set member operation if the target dynamic object cannot bind.
78         /// </summary>
79         /// <param name="target">The target of the dynamic set member operation.</param>
80         /// <param name="value">The value to set to the member.</param>
81         /// <param name="errorSuggestion">The binding result to use if binding fails, or null.</param>
82         /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion)83         public abstract DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion);
84     }
85 }
86