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
|
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// THE ENTIRE RISK OF USE OR RESULTS IN CONNECTION WITH THE USE OF THIS CODE
// AND INFORMATION REMAINS WITH THE USER.
/*********************************************************************
* NOTE: A copy of this file exists at: WF\Activities\Common
* The two files must be kept in sync. Any change made here must also
* be made to WF\Activities\Common\Walker.cs
*********************************************************************/
namespace System.Workflow.ComponentModel
{
#region Imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
#endregion
// Returns true to continue the walk, false to stop.
internal delegate void WalkerEventHandler(Walker walker, WalkerEventArgs eventArgs);
internal enum WalkerAction
{
Continue = 0,
Skip = 1,
Abort = 2
}
#region Class WalkerEventArgs
internal sealed class WalkerEventArgs : EventArgs
{
private Activity currentActivity = null;
private object currentPropertyOwner = null;
private PropertyInfo currentProperty = null;
private object currentValue = null;
private WalkerAction action = WalkerAction.Continue;
internal WalkerEventArgs(Activity currentActivity)
{
this.currentActivity = currentActivity;
this.currentPropertyOwner = null;
this.currentProperty = null;
this.currentValue = null;
}
internal WalkerEventArgs(Activity currentActivity, object currentValue, PropertyInfo currentProperty, object currentPropertyOwner)
: this(currentActivity)
{
this.currentPropertyOwner = currentPropertyOwner;
this.currentProperty = currentProperty;
this.currentValue = currentValue;
}
public WalkerAction Action
{
get
{
return this.action;
}
set
{
this.action = value;
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public PropertyInfo CurrentProperty
{
get
{
return this.currentProperty;
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public object CurrentPropertyOwner
{
get
{
return this.currentPropertyOwner;
}
}
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public object CurrentValue
{
get
{
return this.currentValue;
}
}
public Activity CurrentActivity
{
get
{
return this.currentActivity;
}
}
}
#endregion
internal sealed class Walker
{
#region Members
internal event WalkerEventHandler FoundActivity;
internal event WalkerEventHandler FoundProperty;
private bool useEnabledActivities = false;
#endregion
#region Methods
public Walker()
: this(false)
{
}
public Walker(bool useEnabledActivities)
{
this.useEnabledActivities = useEnabledActivities;
}
public void Walk(Activity seedActivity)
{
Walk(seedActivity, true);
}
public void Walk(Activity seedActivity, bool walkChildren)
{
Queue queue = new Queue();
queue.Enqueue(seedActivity);
while (queue.Count > 0)
{
Activity activity = queue.Dequeue() as Activity;
if (FoundActivity != null)
{
WalkerEventArgs args = new WalkerEventArgs(activity);
FoundActivity(this, args);
if (args.Action == WalkerAction.Abort)
return;
if (args.Action == WalkerAction.Skip)
continue;
}
if (FoundProperty != null)
{
if (!WalkProperties(activity))
return;
}
if (walkChildren && activity is CompositeActivity)
{
if (useEnabledActivities)
{
foreach (Activity activity2 in Design.Helpers.GetAllEnabledActivities((CompositeActivity)activity))
queue.Enqueue(activity2);
}
else
{
foreach (Activity activity2 in ((CompositeActivity)activity).Activities)
queue.Enqueue(activity2);
}
}
}
}
private bool WalkProperties(Activity seedActivity)
{
return WalkProperties(seedActivity as Activity, seedActivity);
}
public bool WalkProperties(Activity activity, object obj)
{
Activity currentActivity = obj as Activity;
PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
// !!Work around: no indexer property walking
if (prop.GetIndexParameters() != null && prop.GetIndexParameters().Length > 0)
continue;
DesignerSerializationVisibility visibility = GetSerializationVisibility(prop);
if (visibility == DesignerSerializationVisibility.Hidden)
continue;
//Try to see if we have dynamic property associated with the object on the same object
//if so then we should compare if the dynamic property values match with the property type
//if not we bail out
object propValue = null;
DependencyProperty dependencyProperty = DependencyProperty.FromName(prop.Name, obj.GetType());
if (dependencyProperty != null && currentActivity != null)
{
if (currentActivity.IsBindingSet(dependencyProperty))
propValue = currentActivity.GetBinding(dependencyProperty);
else
propValue = currentActivity.GetValue(dependencyProperty);
}
else
{
try
{
propValue = prop.CanRead ? prop.GetValue(obj, null) : null;
}
catch
{
// Eat exceptions that occur while invoking the getter.
}
}
if (FoundProperty != null)
{
WalkerEventArgs args = new WalkerEventArgs(activity, propValue, prop, obj);
FoundProperty(this, args);
if (args.Action == WalkerAction.Skip)
continue;
else if (args.Action == WalkerAction.Abort)
return false;
}
if (propValue is IList)
{
//We do not need to reflect on the properties of the list
foreach (object childObj in (IList)propValue)
{
if (FoundProperty != null)
{
WalkerEventArgs args = new WalkerEventArgs(activity, childObj, null, propValue);
FoundProperty(this, args);
if (args.Action == WalkerAction.Skip)
continue;
else if (args.Action == WalkerAction.Abort)
return false;
}
if (childObj != null && IsBrowsableType(childObj.GetType()))
{
if (!WalkProperties(activity, childObj))
return false;
}
}
}
else if (propValue != null && IsBrowsableType(propValue.GetType()))
{
if (!WalkProperties(activity, propValue))
return false;
}
}
return true;
}
private static DesignerSerializationVisibility GetSerializationVisibility(PropertyInfo prop)
{
// work around!!! for Activities collection
if (prop.DeclaringType == typeof(CompositeActivity) && string.Equals(prop.Name, "Activities", StringComparison.Ordinal))
return DesignerSerializationVisibility.Hidden;
DesignerSerializationVisibility visibility = DesignerSerializationVisibility.Visible;
DesignerSerializationVisibilityAttribute[] visibilityAttrs = (DesignerSerializationVisibilityAttribute[])prop.GetCustomAttributes(typeof(DesignerSerializationVisibilityAttribute), true);
if (visibilityAttrs.Length > 0)
visibility = visibilityAttrs[0].Visibility;
return visibility;
}
private static bool IsBrowsableType(Type type)
{
bool browsable = false;
BrowsableAttribute[] browsableAttrs = (BrowsableAttribute[])type.GetCustomAttributes(typeof(BrowsableAttribute), true);
if (browsableAttrs.Length > 0)
browsable = browsableAttrs[0].Browsable;
return browsable;
}
#endregion
}
}
|