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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities.XamlIntegration
{
using System;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Runtime;
using System.Runtime;
using System.Runtime.Serialization;
using System.Reflection;
using System.Collections.Generic;
[DataContract(Name = XD.CompiledLocation.Name, Namespace = XD.Runtime.Namespace)]
internal class CompiledLocation<T> : Location<T>
{
Func<T> getMethod;
Action<T> setMethod;
int expressionId;
IList<LocationReference> locationReferences;
IList<Location> locations;
ActivityInstance rootInstance;
Activity compiledRootActivity;
byte[] compiledRootActivityQualifiedId;
Activity expressionActivity;
byte[] expressionActivityQualifiedId;
string expressionText;
bool forImplementation;
public CompiledLocation(Func<T> getMethod, Action<T> setMethod, IList<LocationReference> locationReferences, IList<Location> locations, int expressionId, Activity compiledRootActivity, ActivityContext currentActivityContext)
{
this.getMethod = getMethod;
this.setMethod = setMethod;
this.forImplementation = currentActivityContext.Activity.MemberOf != currentActivityContext.Activity.RootActivity.MemberOf;
this.locationReferences = locationReferences;
this.locations = locations;
this.expressionId = expressionId;
this.compiledRootActivity = compiledRootActivity;
this.expressionActivity = currentActivityContext.Activity;
//
// Save the root activity instance to get the root activity post persistence
// The root will always be alive as long as the location is valid, which is not
// true for the activity instance of the expression that is executing
this.rootInstance = currentActivityContext.CurrentInstance;
while (this.rootInstance.Parent != null)
{
this.rootInstance = this.rootInstance.Parent;
}
//
// Save the text of the expression for exception message
ITextExpression textExpression = currentActivityContext.Activity as ITextExpression;
if (textExpression != null)
{
this.expressionText = textExpression.ExpressionText;
}
}
public CompiledLocation(Func<T> getMethod, Action<T> setMethod)
{
//
// This is the constructor that is used to refresh the get/set methods during rehydration
// An instance of this class created with the constructor cannot be invoked.
this.getMethod = getMethod;
this.setMethod = setMethod;
}
public override T Value
{
get
{
if (this.getMethod == null)
{
RefreshAccessors();
}
return getMethod();
}
set
{
if (this.setMethod == null)
{
RefreshAccessors();
}
setMethod(value);
}
}
[DataMember(EmitDefaultValue = false)]
public byte[] CompiledRootActivityQualifiedId
{
get
{
if (this.compiledRootActivityQualifiedId == null)
{
return this.compiledRootActivity.QualifiedId.AsByteArray();
}
return this.compiledRootActivityQualifiedId;
}
set
{
this.compiledRootActivityQualifiedId = value;
}
}
[DataMember(EmitDefaultValue = false)]
public byte[] ExpressionActivityQualifiedId
{
get
{
if (this.expressionActivityQualifiedId == null)
{
return this.expressionActivity.QualifiedId.AsByteArray();
}
return this.expressionActivityQualifiedId;
}
set
{
this.expressionActivityQualifiedId = value;
}
}
[DataMember(EmitDefaultValue = false)]
public IList<Tuple<string, Type>> locationReferenceCache
{
get
{
if (this.locationReferences == null || this.locationReferences.Count == 0)
{
return null;
}
List<Tuple<string, Type>> durableCache = new List<Tuple<string, Type>>(this.locationReferences.Count);
foreach (LocationReference reference in locationReferences)
{
durableCache.Add(new Tuple<string, Type>(reference.Name, reference.Type));
}
return durableCache;
}
set
{
if (value == null || value.Count == 0)
{
this.locationReferences = new List<LocationReference>();
return;
}
this.locationReferences = new List<LocationReference>(value.Count);
foreach (Tuple<string, Type> reference in value)
{
this.locationReferences.Add(new CompiledLocationReference(reference.Item1, reference.Item2));
}
}
}
[DataMember(EmitDefaultValue = false, Name = "expressionId")]
internal int SerializedExpressionId
{
get { return this.expressionId; }
set { this.expressionId = value; }
}
[DataMember(EmitDefaultValue = false, Name = "locations")]
internal IList<Location> SerializedLocations
{
get { return this.locations; }
set { this.locations = value; }
}
[DataMember(EmitDefaultValue = false, Name = "rootInstance")]
internal ActivityInstance SerializedRootInstance
{
get { return this.rootInstance; }
set { this.rootInstance = value; }
}
[DataMember(EmitDefaultValue = false, Name = "expressionText")]
internal string SerializedExpressionText
{
get { return this.expressionText; }
set { this.expressionText = value; }
}
[DataMember(EmitDefaultValue = false, Name = "forImplementation")]
internal bool SerializedForImplementation
{
get { return this.forImplementation; }
set { this.forImplementation = value; }
}
void RefreshAccessors()
{
//
// If we've gotten here is means that we have a location that has roundtripped through persistence
// CompiledDataContext & ICER don't round trip so we need to get them back from the current tree
// and get new pointers to the get/set methods for this expression
ICompiledExpressionRoot compiledRoot = GetCompiledExpressionRoot();
CompiledLocation<T> tempLocation = (CompiledLocation<T>)compiledRoot.InvokeExpression(this.expressionId, this.locations);
this.getMethod = tempLocation.getMethod;
this.setMethod = tempLocation.setMethod;
}
ICompiledExpressionRoot GetCompiledExpressionRoot()
{
if (this.rootInstance != null && this.rootInstance.Activity != null)
{
ICompiledExpressionRoot compiledExpressionRoot;
Activity rootActivity = this.rootInstance.Activity;
Activity compiledRootActivity = null;
Activity expressionActivity = null;
if (QualifiedId.TryGetElementFromRoot(rootActivity, this.compiledRootActivityQualifiedId, out compiledRootActivity) &&
QualifiedId.TryGetElementFromRoot(rootActivity, this.expressionActivityQualifiedId, out expressionActivity))
{
if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(expressionActivity, compiledRootActivity, out compiledExpressionRoot))
{
//
// Revalidate to make sure we didn't hit an ID shift
if (compiledExpressionRoot.CanExecuteExpression(this.expressionText, true /* this is always a reference */, this.locationReferences, out this.expressionId))
{
return compiledExpressionRoot;
}
}
}
//
// We were valid when this location was generated so an ID shift occurred (likely due to a dynamic update)
// Need to search all of the ICERs for one that can execute this expression.
if (FindCompiledExpressionRoot(rootActivity, out compiledExpressionRoot))
{
return compiledExpressionRoot;
}
}
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnableToLocateCompiledLocationContext(this.expressionText)));
}
bool FindCompiledExpressionRoot(Activity activity, out ICompiledExpressionRoot compiledExpressionRoot)
{
if (CompiledExpressionInvoker.TryGetCompiledExpressionRoot(activity, this.forImplementation, out compiledExpressionRoot))
{
if (compiledExpressionRoot.CanExecuteExpression(this.expressionText, true /* this is always a reference */, this.locationReferences, out this.expressionId))
{
return true;
}
}
foreach (Activity containedActivity in WorkflowInspectionServices.GetActivities(activity))
{
if (FindCompiledExpressionRoot(containedActivity, out compiledExpressionRoot))
{
return true;
}
}
compiledExpressionRoot = null;
return false;
}
class CompiledLocationReference : LocationReference
{
string name;
Type type;
public CompiledLocationReference(string name, Type type)
{
this.name = name;
this.type = type;
}
protected override string NameCore
{
get
{
return name;
}
}
protected override Type TypeCore
{
get
{
return type;
}
}
public override Location GetLocation(ActivityContext context)
{
//
// We should never hit this, these references are strictly for preserving location names/types
// through persistence to allow for revalidation on the other side
// Actual execution occurs through the locations that were stored separately
throw FxTrace.Exception.AsError(new InvalidOperationException(SR.CompiledLocationReferenceGetLocation));
}
}
}
}
|