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
|
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.Activities
{
using System.Activities.Validation;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime;
using System.Windows.Markup;
using System.Collections.ObjectModel;
[SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldNotHaveIncorrectSuffix,
Justification = "Part of the sanctioned, public WF OM")]
[ContentProperty("Handler")]
public abstract class ActivityDelegate
{
internal static string ArgumentName = "Argument";
internal static string Argument1Name = "Argument1";
internal static string Argument2Name = "Argument2";
internal static string Argument3Name = "Argument3";
internal static string Argument4Name = "Argument4";
internal static string Argument5Name = "Argument5";
internal static string Argument6Name = "Argument6";
internal static string Argument7Name = "Argument7";
internal static string Argument8Name = "Argument8";
internal static string Argument9Name = "Argument9";
internal static string Argument10Name = "Argument10";
internal static string Argument11Name = "Argument11";
internal static string Argument12Name = "Argument12";
internal static string Argument13Name = "Argument13";
internal static string Argument14Name = "Argument14";
internal static string Argument15Name = "Argument15";
internal static string Argument16Name = "Argument16";
internal static string ResultArgumentName = "Result";
Activity owner;
bool isDisplayNameSet;
string displayName;
IList<RuntimeDelegateArgument> delegateParameters;
int cacheId;
ActivityCollectionType parentCollectionType;
protected ActivityDelegate()
{
}
public string DisplayName
{
get
{
if (string.IsNullOrEmpty(this.displayName))
{
this.displayName = this.GetType().Name;
}
return this.displayName;
}
set
{
this.isDisplayNameSet = true;
this.displayName = value;
}
}
[DefaultValue(null)]
public Activity Handler
{
get;
set;
}
internal LocationReferenceEnvironment Environment
{
get;
set;
}
internal Activity Owner
{
get
{
return this.owner;
}
}
internal ActivityCollectionType ParentCollectionType
{
get
{
return this.parentCollectionType;
}
}
internal IList<RuntimeDelegateArgument> RuntimeDelegateArguments
{
get
{
if (this.delegateParameters != null)
{
return this.delegateParameters;
}
return new ReadOnlyCollection<RuntimeDelegateArgument>(InternalGetRuntimeDelegateArguments());
}
}
protected internal virtual DelegateOutArgument GetResultArgument()
{
return null;
}
protected virtual void OnGetRuntimeDelegateArguments(IList<RuntimeDelegateArgument> runtimeDelegateArguments)
{
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(this))
{
ArgumentDirection direction;
Type innerType;
if (ActivityUtilities.TryGetDelegateArgumentDirectionAndType(propertyDescriptor.PropertyType, out direction, out innerType))
{
runtimeDelegateArguments.Add(new RuntimeDelegateArgument(propertyDescriptor.Name, innerType, direction, (DelegateArgument)propertyDescriptor.GetValue(this)));
}
}
}
internal virtual IList<RuntimeDelegateArgument> InternalGetRuntimeDelegateArguments()
{
IList<RuntimeDelegateArgument> result = new List<RuntimeDelegateArgument>();
OnGetRuntimeDelegateArguments(result);
return result;
}
internal void InternalCacheMetadata()
{
this.delegateParameters = new ReadOnlyCollection<RuntimeDelegateArgument>(InternalGetRuntimeDelegateArguments());
}
internal bool CanBeScheduledBy(Activity parent)
{
// fast path if we're the sole (or first) child
if (object.ReferenceEquals(parent, this.owner))
{
return this.parentCollectionType != ActivityCollectionType.Imports;
}
else
{
return parent.Delegates.Contains(this) || parent.ImplementationDelegates.Contains(this);
}
}
internal bool InitializeRelationship(Activity parent, ActivityCollectionType collectionType, ref IList<ValidationError> validationErrors)
{
if (this.cacheId == parent.CacheId)
{
Fx.Assert(this.owner != null, "We must have set the owner when we set the cache ID");
// This means that we've already encountered a parent in the tree
// Validate that it is visible.
// In order to see the activity the new parent must be
// in the implementation IdSpace of an activity which has
// a public reference to it.
Activity referenceTarget = parent.MemberOf.Owner;
if (referenceTarget == null)
{
Activity handler = this.Handler;
if (handler == null)
{
ActivityUtilities.Add(ref validationErrors, new ValidationError(SR.ActivityDelegateCannotBeReferencedWithoutTargetNoHandler(parent.DisplayName, this.owner.DisplayName), false, parent));
}
else
{
ActivityUtilities.Add(ref validationErrors, new ValidationError(SR.ActivityDelegateCannotBeReferencedWithoutTarget(handler.DisplayName, parent.DisplayName, this.owner.DisplayName), false, parent));
}
return false;
}
else if (!referenceTarget.Delegates.Contains(this) && !referenceTarget.ImportedDelegates.Contains(this))
{
Activity handler = this.Handler;
if (handler == null)
{
ActivityUtilities.Add(ref validationErrors, new ValidationError(SR.ActivityDelegateCannotBeReferencedNoHandler(parent.DisplayName, referenceTarget.DisplayName, this.owner.DisplayName), false, parent));
}
else
{
ActivityUtilities.Add(ref validationErrors, new ValidationError(SR.ActivityDelegateCannotBeReferenced(handler.DisplayName, parent.DisplayName, referenceTarget.DisplayName, this.owner.DisplayName), false, parent));
}
return false;
}
// This is a valid reference so we want to allow
// normal processing to proceed.
return true;
}
this.owner = parent;
this.cacheId = parent.CacheId;
this.parentCollectionType = collectionType;
InternalCacheMetadata();
// We need to setup the delegate environment so that it is
// available when we process the Handler.
LocationReferenceEnvironment delegateEnvironment = null;
if (collectionType == ActivityCollectionType.Implementation)
{
delegateEnvironment = parent.ImplementationEnvironment;
}
else
{
delegateEnvironment = parent.PublicEnvironment;
}
if (this.RuntimeDelegateArguments.Count > 0)
{
ActivityLocationReferenceEnvironment newEnvironment = new ActivityLocationReferenceEnvironment(delegateEnvironment);
delegateEnvironment = newEnvironment;
for (int argumentIndex = 0; argumentIndex < this.RuntimeDelegateArguments.Count; argumentIndex++)
{
RuntimeDelegateArgument runtimeDelegateArgument = this.RuntimeDelegateArguments[argumentIndex];
DelegateArgument delegateArgument = runtimeDelegateArgument.BoundArgument;
if (delegateArgument != null)
{
if (delegateArgument.Direction != runtimeDelegateArgument.Direction)
{
ActivityUtilities.Add(ref validationErrors, new ValidationError(SR.RuntimeDelegateArgumentDirectionIncorrect, parent));
}
if (delegateArgument.Type != runtimeDelegateArgument.Type)
{
ActivityUtilities.Add(ref validationErrors, new ValidationError(SR.RuntimeDelegateArgumentTypeIncorrect, parent));
}
// NOTE: We don't initialize this relationship here because
// at runtime we'll actually just place these variables in the
// environment of the Handler. We'll initialize and set an
// ID when we process the Handler.
newEnvironment.Declare(delegateArgument, this.owner, ref validationErrors);
}
}
}
this.Environment = delegateEnvironment;
if (this.Handler != null)
{
return this.Handler.InitializeRelationship(this, collectionType, ref validationErrors);
}
return true;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDisplayName()
{
return this.isDisplayNameSet;
}
public override string ToString()
{
return this.DisplayName;
}
}
}
|