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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities
{
using System;
using System.Activities.Runtime;
using System.Activities.Tracking;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime;
[Fx.Tag.XamlVisible(false)]
public class ActivityContext
{
ActivityInstance instance;
ActivityExecutor executor;
bool isDisposed;
long instanceId;
// Used by subclasses that are pooled.
internal ActivityContext()
{
}
// these can only be created by the WF Runtime
internal ActivityContext(ActivityInstance instance, ActivityExecutor executor)
{
Fx.Assert(instance != null, "valid activity instance is required");
this.instance = instance;
this.executor = executor;
this.Activity = this.instance.Activity;
this.instanceId = instance.InternalId;
}
internal LocationEnvironment Environment
{
get
{
ThrowIfDisposed();
return this.instance.Environment;
}
}
internal bool AllowChainedEnvironmentAccess
{
get;
set;
}
internal Activity Activity
{
get;
private set;
}
internal ActivityInstance CurrentInstance
{
get
{
return this.instance;
}
}
internal ActivityExecutor CurrentExecutor
{
get
{
return this.executor;
}
}
public string ActivityInstanceId
{
get
{
ThrowIfDisposed();
return this.instanceId.ToString(CultureInfo.InvariantCulture);
}
}
public Guid WorkflowInstanceId
{
get
{
ThrowIfDisposed();
return this.executor.WorkflowInstanceId;
}
}
public WorkflowDataContext DataContext
{
get
{
ThrowIfDisposed();
// Argument expressions don't have visbility into public variables at the same scope.
// However fast-path expressions use the parent's ActivityInstance instead of
// creating their own, so we need to give them a DataContext without variables
bool includeLocalVariables = !this.instance.IsResolvingArguments;
if (this.instance.DataContext == null ||
this.instance.DataContext.IncludesLocalVariables != includeLocalVariables)
{
this.instance.DataContext
= new WorkflowDataContext(this.executor, this.instance, includeLocalVariables);
}
return this.instance.DataContext;
}
}
internal bool IsDisposed
{
get
{
return this.isDisposed;
}
}
public T GetExtension<T>()
where T : class
{
ThrowIfDisposed();
return this.executor.GetExtension<T>();
}
internal Location GetIgnorableResultLocation(RuntimeArgument resultArgument)
{
return this.executor.GetIgnorableResultLocation(resultArgument);
}
internal void Reinitialize(ActivityInstance instance, ActivityExecutor executor)
{
Reinitialize(instance, executor, instance.Activity, instance.InternalId);
}
internal void Reinitialize(ActivityInstance instance, ActivityExecutor executor, Activity activity, long instanceId)
{
this.isDisposed = false;
this.instance = instance;
this.executor = executor;
this.Activity = activity;
this.instanceId = instanceId;
}
// extra insurance against misuse (if someone stashes away the execution context to use later)
internal void Dispose()
{
this.isDisposed = true;
this.instance = null;
this.executor = null;
this.Activity = null;
this.instanceId = 0;
}
internal void DisposeDataContext()
{
if (this.instance.DataContext != null)
{
this.instance.DataContext.DisposeEnvironment();
this.instance.DataContext = null;
}
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
public Location<T> GetLocation<T>(LocationReference locationReference)
{
ThrowIfDisposed();
if (locationReference == null)
{
throw FxTrace.Exception.ArgumentNull("locationReference");
}
Location location = locationReference.GetLocation(this);
Location<T> typedLocation = location as Location<T>;
if (typedLocation != null)
{
return typedLocation;
}
else
{
Fx.Assert(location != null, "The contract of LocationReference is that GetLocation never returns null.");
if (locationReference.Type == typeof(T))
{
return new TypedLocationWrapper<T>(location);
}
else
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.LocationTypeMismatch(locationReference.Name, typeof(T), locationReference.Type)));
}
}
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
public T GetValue<T>(LocationReference locationReference)
{
ThrowIfDisposed();
if (locationReference == null)
{
throw FxTrace.Exception.ArgumentNull("locationReference");
}
return GetValueCore<T>(locationReference);
}
internal T GetValueCore<T>(LocationReference locationReference)
{
Location location = locationReference.GetLocationForRead(this);
Location<T> typedLocation = location as Location<T>;
if (typedLocation != null)
{
// If we hit this path we can avoid boxing value types
return typedLocation.Value;
}
else
{
Fx.Assert(location != null, "The contract of LocationReference is that GetLocation never returns null.");
return TypeHelper.Convert<T>(location.Value);
}
}
public void SetValue<T>(LocationReference locationReference, T value)
{
ThrowIfDisposed();
if (locationReference == null)
{
throw FxTrace.Exception.ArgumentNull("locationReference");
}
SetValueCore<T>(locationReference, value);
}
internal void SetValueCore<T>(LocationReference locationReference, T value)
{
Location location = locationReference.GetLocationForWrite(this);
Location<T> typedLocation = location as Location<T>;
if (typedLocation != null)
{
// If we hit this path we can avoid boxing value types
typedLocation.Value = value;
}
else
{
if (!TypeHelper.AreTypesCompatible(value, locationReference.Type))
{
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CannotSetValueToLocation(value != null ? value.GetType() : typeof(T), locationReference.Name, locationReference.Type)));
}
location.Value = value;
}
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "Generic needed for type inference")]
public T GetValue<T>(OutArgument<T> argument)
{
ThrowIfDisposed();
if (argument == null)
{
throw FxTrace.Exception.ArgumentNull("argument");
}
argument.ThrowIfNotInTree();
return GetValueCore<T>(argument.RuntimeArgument);
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "Generic needed for type inference")]
public T GetValue<T>(InOutArgument<T> argument)
{
ThrowIfDisposed();
if (argument == null)
{
throw FxTrace.Exception.ArgumentNull("argument");
}
argument.ThrowIfNotInTree();
return GetValueCore<T>(argument.RuntimeArgument);
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "Generic needed for type inference")]
public T GetValue<T>(InArgument<T> argument)
{
ThrowIfDisposed();
if (argument == null)
{
throw FxTrace.Exception.ArgumentNull("argument");
}
argument.ThrowIfNotInTree();
return GetValueCore<T>(argument.RuntimeArgument);
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
public object GetValue(Argument argument)
{
ThrowIfDisposed();
if (argument == null)
{
throw FxTrace.Exception.ArgumentNull("argument");
}
argument.ThrowIfNotInTree();
return GetValueCore<object>(argument.RuntimeArgument);
}
// Soft-Link: This method is referenced through reflection by
// ExpressionUtilities.TryRewriteLambdaExpression. Update that
// file if the signature changes.
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "We explicitly provide a RuntimeArgument overload to avoid requiring the object type parameter.")]
public object GetValue(RuntimeArgument runtimeArgument)
{
ThrowIfDisposed();
if (runtimeArgument == null)
{
throw FxTrace.Exception.ArgumentNull("runtimeArgument");
}
return GetValueCore<object>(runtimeArgument);
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "Generic needed for type inference")]
public void SetValue<T>(OutArgument<T> argument, T value)
{
ThrowIfDisposed();
if (argument == null)
{
// We want to shortcut if the argument is null
return;
}
argument.ThrowIfNotInTree();
SetValueCore(argument.RuntimeArgument, value);
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "Generic needed for type inference")]
public void SetValue<T>(InOutArgument<T> argument, T value)
{
ThrowIfDisposed();
if (argument == null)
{
// We want to shortcut if the argument is null
return;
}
argument.ThrowIfNotInTree();
SetValueCore(argument.RuntimeArgument, value);
}
[SuppressMessage(FxCop.Category.Design, FxCop.Rule.ConsiderPassingBaseTypesAsParameters,
Justification = "Generic needed for type inference")]
public void SetValue<T>(InArgument<T> argument, T value)
{
ThrowIfDisposed();
if (argument == null)
{
// We want to shortcut if the argument is null
return;
}
argument.ThrowIfNotInTree();
SetValueCore(argument.RuntimeArgument, value);
}
public void SetValue(Argument argument, object value)
{
ThrowIfDisposed();
if (argument == null)
{
throw FxTrace.Exception.ArgumentNull("argument");
}
argument.ThrowIfNotInTree();
SetValueCore(argument.RuntimeArgument, value);
}
internal void TrackCore(CustomTrackingRecord record)
{
Fx.Assert(!this.isDisposed, "not usable if disposed");
Fx.Assert(record != null, "expect non-null record");
if (this.executor.ShouldTrack)
{
record.Activity = new ActivityInfo(this.instance);
record.InstanceId = this.WorkflowInstanceId;
this.executor.AddTrackingRecord(record);
}
}
internal void ThrowIfDisposed()
{
if (this.isDisposed)
{
throw FxTrace.Exception.AsError(
new ObjectDisposedException(this.GetType().FullName, SR.AECDisposed));
}
}
}
}
|