File: Argument.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (344 lines) | stat: -rw-r--r-- 11,325 bytes parent folder | download | duplicates (7)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace System.Activities
{
    using System;
    using System.Activities.Runtime;
    using System.Activities.Validation;
    using System.Activities.XamlIntegration;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Runtime;
    using System.Runtime.Serialization;
    using System.Windows.Markup;

    public abstract class Argument
    {
        public static readonly int UnspecifiedEvaluationOrder = -1;

        public const string ResultValue = "Result";
        
        ArgumentDirection direction;
        RuntimeArgument runtimeArgument;
        int evaluationOrder;

        internal Argument()
        {
            this.evaluationOrder = Argument.UnspecifiedEvaluationOrder;
        }

        public Type ArgumentType
        {
            get;
            internal set;
        }

        public ArgumentDirection Direction
        {
            get
            {
                return this.direction;
            }
            internal set
            {
                ArgumentDirectionHelper.Validate(value, "value");
                this.direction = value;
            }
        }

        [DefaultValue(-1)]
        public int EvaluationOrder
        {
            get
            {
                return this.evaluationOrder;
            }
            set
            {
                if (value < 0 && value != Argument.UnspecifiedEvaluationOrder)
                {
                    throw FxTrace.Exception.ArgumentOutOfRange("EvaluationOrder", value, SR.InvalidEvaluationOrderValue);
                }
                this.evaluationOrder = value;
            }
        }

        [IgnoreDataMember] // this member is repeated by all subclasses, which we control
        [DefaultValue(null)]
        public ActivityWithResult Expression
        {
            get
            {
                return this.ExpressionCore;
            }
            set
            {
                this.ExpressionCore = value;
            }
        }        

        internal abstract ActivityWithResult ExpressionCore
        {
            get;
            set;
        }

        internal RuntimeArgument RuntimeArgument
        {
            get
            {
                return this.runtimeArgument;
            }
            set
            {
                this.runtimeArgument = value;
            }
        }

        internal bool IsInTree
        {
            get
            {
                return (this.runtimeArgument != null && this.runtimeArgument.IsInTree);
            }
        }

        internal bool WasDesignTimeNull
        {
            get;
            set;
        }

        internal int Id
        {
            get
            {
                Fx.Assert(this.runtimeArgument != null, "We shouldn't call Id unless we have a runtime argument.");
                return this.runtimeArgument.Id;
            }
        }

        internal bool IsEmpty
        {
            get
            {
                return this.Expression == null;
            }
        }

        public static Argument CreateReference(Argument argumentToReference, string referencedArgumentName)
        {
            if (argumentToReference == null)
            {
                throw FxTrace.Exception.ArgumentNull("argumentToReference");
            }

            if (string.IsNullOrEmpty(referencedArgumentName))
            {
                throw FxTrace.Exception.ArgumentNullOrEmpty("referencedArgumentName");
            }

            return ActivityUtilities.CreateReferenceArgument(argumentToReference.ArgumentType, argumentToReference.Direction, referencedArgumentName);
        }

        // for ArgumentValueSerializer
        internal bool CanConvertToString(IValueSerializerContext context)
        {
            if (this.WasDesignTimeNull)
            {
                return true;
            }            
            else
            {
                if (this.EvaluationOrder == Argument.UnspecifiedEvaluationOrder)
                {
                    return ActivityWithResultValueSerializer.CanConvertToStringWrapper(this.Expression, context);
                }
                else
                {
                    return false;
                }
            }
        }

        internal string ConvertToString(IValueSerializerContext context)
        {
            if (this.WasDesignTimeNull)
            {
                // this argument instance was artificially created by the runtime
                // to Xaml, this should appear as {x:Null}
                return null;
            }

            return ActivityWithResultValueSerializer.ConvertToStringWrapper(this.Expression, context);
        }

        internal static void Bind(Argument binding, RuntimeArgument argument)
        {
            if (binding != null)
            {
                Fx.Assert(binding.Direction == argument.Direction, "The directions must match.");
                Fx.Assert(binding.ArgumentType == argument.Type, "The types must match.");

                binding.RuntimeArgument = argument;
            }

            argument.BoundArgument = binding;
        }

        internal static void TryBind(Argument binding, RuntimeArgument argument, Activity violationOwner)
        {
            if (argument == null)
            {
                throw FxTrace.Exception.ArgumentNull("argument");
            }

            bool passedValidations = true;

            if (binding != null)
            {
                if (binding.Direction != argument.Direction)
                {
                    violationOwner.AddTempValidationError(new ValidationError(SR.ArgumentDirectionMismatch(argument.Name, argument.Direction, binding.Direction)));
                    passedValidations = false;
                }

                if (binding.ArgumentType != argument.Type)
                {
                    violationOwner.AddTempValidationError(new ValidationError(SR.ArgumentTypeMismatch(argument.Name, argument.Type, binding.ArgumentType)));
                    passedValidations = false;
                }
            }

            if (passedValidations)
            {
                Bind(binding, argument);
            }
        }

        public static Argument Create(Type type, ArgumentDirection direction)
        {
            return ActivityUtilities.CreateArgument(type, direction);
        }

        internal abstract Location CreateDefaultLocation();

        internal abstract void Declare(LocationEnvironment targetEnvironment, ActivityInstance activityInstance);

        // Soft-Link: This method is referenced through reflection by
        // ExpressionUtilities.TryRewriteLambdaExpression.  Update that
        // file if the signature changes.
        public object Get(ActivityContext context)
        {
            return Get<object>(context);
        }

        // Soft-Link: This method is referenced through reflection by
        // ExpressionUtilities.TryRewriteLambdaExpression.  Update that
        // file if the signature changes.
        public T Get<T>(ActivityContext context)
        {
            if (context == null)
            {
                throw FxTrace.Exception.ArgumentNull("context");
            }

            ThrowIfNotInTree();

            return context.GetValue<T>(this.RuntimeArgument);
        }

        public void Set(ActivityContext context, object value)
        {
            if (context == null)
            {
                throw FxTrace.Exception.ArgumentNull("context");
            }

            ThrowIfNotInTree();

            context.SetValue(this.RuntimeArgument, value);
        }

        internal void Validate(Activity owner, ref IList<ValidationError> validationErrors)
        {
            if (this.Expression != null)
            {
                if (this.Expression.Result != null && !this.Expression.Result.IsEmpty)
                {
                    ValidationError validationError = new ValidationError(SR.ResultCannotBeSetOnArgumentExpressions, false, this.RuntimeArgument.Name, owner);
                    ActivityUtilities.Add(ref validationErrors, validationError);
                }

                ActivityWithResult actualExpression = this.Expression;

                if (actualExpression is IExpressionWrapper)
                {
                    actualExpression = ((IExpressionWrapper)actualExpression).InnerExpression;
                }

                switch (this.Direction)
                {
                    case ArgumentDirection.In:
                        if (actualExpression.ResultType != this.ArgumentType)
                        {
                            ActivityUtilities.Add(
                                ref validationErrors,
                                new ValidationError(SR.ArgumentValueExpressionTypeMismatch(this.ArgumentType, actualExpression.ResultType), false, this.RuntimeArgument.Name, owner));
                        }
                        break;
                    case ArgumentDirection.InOut:
                    case ArgumentDirection.Out:
                        Type locationType;
                        if (!ActivityUtilities.IsLocationGenericType(actualExpression.ResultType, out locationType) ||
                            locationType != this.ArgumentType)
                        {
                            Type expectedType = ActivityUtilities.CreateActivityWithResult(ActivityUtilities.CreateLocation(this.ArgumentType));
                            ActivityUtilities.Add(
                                ref validationErrors,
                                new ValidationError(SR.ArgumentLocationExpressionTypeMismatch(expectedType.FullName, actualExpression.GetType().FullName), false, this.RuntimeArgument.Name, owner));
                        }
                        break;
                }
            }
        }

        // optional "fast-path" for arguments that can be resolved synchronously
        internal abstract bool TryPopulateValue(LocationEnvironment targetEnvironment, ActivityInstance targetActivityInstance, ActivityExecutor executor);

        // Soft-Link: This method is referenced through reflection by
        // ExpressionUtilities.TryRewriteLambdaExpression.  Update that
        // file if the signature changes.
        public Location GetLocation(ActivityContext context)
        {
            if (context == null)
            {
                throw FxTrace.Exception.ArgumentNull("context");
            }

            ThrowIfNotInTree();

            return this.runtimeArgument.GetLocation(context);
        }

        internal void ThrowIfNotInTree()
        {
            if (!this.IsInTree)
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR.ArgumentNotInTree(this.ArgumentType)));
            }
        }

        internal static Location<T> CreateLocation<T>()
        {
            return new Location<T>();
        }

        internal interface IExpressionWrapper
        {
            ActivityWithResult InnerExpression { get; }
        }
    }
}