alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using DynamicExpresso.Reflection;
using Microsoft.CSharp.RuntimeBinder;
 
namespace DynamicExpresso.Resolution
{
    internal interface IConvertibleToWritableBinder
    {
        CallSiteBinder ToWritableBinder();
    }
 
    internal class LateGetMemberCallSiteBinder : CallSiteBinder, IConvertibleToWritableBinder
    {
        private readonly string _propertyOrFieldName;
 
        public LateGetMemberCallSiteBinder(string propertyOrFieldName)
        {
            _propertyOrFieldName = propertyOrFieldName;
        }
 
        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
        {
            // there's only one argument: the instance on which the member is accessed
            var binder = Binder.GetMember(
                CSharpBinderFlags.None,
                _propertyOrFieldName,
                TypeUtils.RemoveArrayType(args[0]?.GetType()),
                parameters.Select(x => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null))
            );
            return binder.Bind(args, parameters, returnLabel);
        }
 
        public CallSiteBinder ToWritableBinder()
        {
            return new LateSetMemberCallSiteBinder(_propertyOrFieldName);
        }
    }
 
    internal class LateSetMemberCallSiteBinder : CallSiteBinder
    {
        private readonly string _propertyOrFieldName;
 
        public LateSetMemberCallSiteBinder(string propertyOrFieldName)
        {
            _propertyOrFieldName = propertyOrFieldName;
        }
 
        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
        {
            // there are two arguments: the instance on which the member is set and the value to set
            var binder = Binder.SetMember(
                CSharpBinderFlags.None,
                _propertyOrFieldName,
                TypeUtils.RemoveArrayType(args[0]?.GetType()),
                parameters.Select(x => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null))
            );
            return binder.Bind(args, parameters, returnLabel);
        }
    }
 
    /// <summary>
    /// Binds to a method invocation of an instance as late as possible.  This allows the use of anonymous types on dynamic values.
    /// </summary>
    internal class LateInvokeMethodCallSiteBinder : CallSiteBinder
    {
        private readonly string _methodName;
        private readonly bool _isStatic;
 
        public LateInvokeMethodCallSiteBinder(string methodName, bool isStatic)
        {
            _methodName = methodName;
            _isStatic = isStatic;
        }
 
        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
        {
            // if the method is static, the first argument is the type containing the method,
            // otherwise it's the instance on which the method is called
            var context = _isStatic ? (Type)args[0] : args[0]?.GetType();
            var argumentInfo = parameters.Select(x => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)).ToArray();
            if (_isStatic)
            {
                // instruct the compiler that we already know the containing type of the method
                argumentInfo[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType | CSharpArgumentInfoFlags.IsStaticType, null);
            }
 
            var binderM = Binder.InvokeMember(
                CSharpBinderFlags.None,
                _methodName,
                null,
                TypeUtils.RemoveArrayType(context),
                argumentInfo
            );
            return binderM.Bind(args, parameters, returnLabel);
        }
    }
 
    /// <summary>
    /// Binds to a delegate invocation as late as possible.  This allows the use of delegates with dynamic arguments.
    /// </summary>
    internal class LateInvokeDelegateCallSiteBinder : CallSiteBinder
    {
        public LateInvokeDelegateCallSiteBinder()
        {
        }
 
        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
        {
            var argumentInfo = parameters.Select(x => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)).ToArray();
 
            // the first argument is the delegate to invoke: instruct the compiler that we already know its type
            argumentInfo[0] = CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.UseCompileTimeType, null);
 
            var binderM = Binder.Invoke(
                CSharpBinderFlags.None,
                null,
                argumentInfo
            );
            return binderM.Bind(args, parameters, returnLabel);
        }
    }
 
    /// <summary>
    /// Binds to an items invocation of an instance as late as possible.  This allows the use of anonymous types on dynamic values.
    /// </summary>
    internal class LateGetIndexCallSiteBinder : CallSiteBinder, IConvertibleToWritableBinder
    {
        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
        {
            // there are two arguments: the instance on which the member is set and the value of the indexer
            var binder = Binder.GetIndex(
                CSharpBinderFlags.None,
                TypeUtils.RemoveArrayType(args[0]?.GetType()),
                parameters.Select(x => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null))
            );
            return binder.Bind(args, parameters, returnLabel);
        }
 
        public CallSiteBinder ToWritableBinder()
        {
            return new LateSetIndexCallSiteBinder();
        }
    }
 
    internal class LateSetIndexCallSiteBinder : CallSiteBinder
    {
        public override Expression Bind(object[] args, ReadOnlyCollection<ParameterExpression> parameters, LabelTarget returnLabel)
        {
            // there are three arguments: the instance on which the member is set, the value of the indexer, and the value to set
            var binder = Binder.SetIndex(
                CSharpBinderFlags.None,
                TypeUtils.RemoveArrayType(args[0]?.GetType()),
                parameters.Select(x => CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null))
            );
            return binder.Bind(args, parameters, returnLabel);
        }
    }
}