private static ActivityBind GetContextBind(ActivityBind activityBind, Activity activity, out Activity contextActivity) { if (activityBind == null) { throw new ArgumentNullException("activityBind"); } if (activity == null) { throw new ArgumentNullException("activity"); } BindRecursionContext context = new BindRecursionContext(); ActivityBind bind = activityBind; contextActivity = activity; while (bind != null) { Activity activity2 = Helpers.ParseActivityForBind(contextActivity, bind.Name); if (activity2 == null) { return(null); } object dataContext = activity2; MemberInfo memberInfo = GetMemberInfo(dataContext.GetType(), bind.Path, null); if (memberInfo == null) { contextActivity = activity2; return(bind); } if (memberInfo is FieldInfo) { contextActivity = activity2; return(bind); } if ((!(memberInfo is PropertyInfo) || !((memberInfo as PropertyInfo).PropertyType == typeof(ActivityBind))) || (dataContext == null)) { return(null); } object obj3 = MemberBind.GetValue(memberInfo, dataContext, bind.Path); if (!(obj3 is ActivityBind)) { return(null); } if (context.Contains(contextActivity, bind)) { return(null); } context.Add(contextActivity, bind); contextActivity = activity2; bind = obj3 as ActivityBind; } return(bind); }
internal static object ResolveActivityPath(Activity refActivity, string path) { PathWalker walker; if (refActivity == null) { throw new ArgumentNullException("refActivity"); } if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException(SR.GetString("Error_EmptyPathValue"), "path"); } object value = refActivity; BindRecursionContext recursionContext = new BindRecursionContext(); if (new PathWalker { MemberFound = (EventHandler<PathMemberInfoEventArgs>) Delegate.Combine(walker.MemberFound, delegate (object sender, PathMemberInfoEventArgs eventArgs) { if (value == null) { eventArgs.Action = PathWalkAction.Cancel; } else { switch (eventArgs.MemberKind) { case PathMemberKind.Field: try { value = (eventArgs.MemberInfo as FieldInfo).GetValue(value); } catch (Exception exception) { value = null; eventArgs.Action = PathWalkAction.Cancel; if (!refActivity.DesignMode) { TargetInvocationException exception2 = exception as TargetInvocationException; throw (exception2 != null) ? exception2.InnerException : exception; } } break; case PathMemberKind.Event: { EventInfo memberInfo = eventArgs.MemberInfo as EventInfo; DependencyProperty dependencyProperty = DependencyProperty.FromName(memberInfo.Name, value.GetType()); if ((dependencyProperty != null) && (value is DependencyObject)) { if ((value as DependencyObject).IsBindingSet(dependencyProperty)) { value = (value as DependencyObject).GetBinding(dependencyProperty); } else { value = (value as DependencyObject).GetHandler(dependencyProperty); } } break; } case PathMemberKind.Property: if ((eventArgs.MemberInfo as PropertyInfo).CanRead) { DependencyProperty property2 = DependencyProperty.FromName(eventArgs.MemberInfo.Name, value.GetType()); if (((property2 != null) && (value is DependencyObject)) && (value as DependencyObject).IsBindingSet(property2)) { value = (value as DependencyObject).GetBinding(property2); } else { try { value = (eventArgs.MemberInfo as PropertyInfo).GetValue(value, null); } catch (Exception exception3) { value = null; eventArgs.Action = PathWalkAction.Cancel; if (!refActivity.DesignMode) { TargetInvocationException exception4 = exception3 as TargetInvocationException; throw (exception4 != null) ? exception4.InnerException : exception3; } } } break; } eventArgs.Action = PathWalkAction.Cancel; return; case PathMemberKind.IndexedProperty: case PathMemberKind.Index: try { value = (eventArgs.MemberInfo as PropertyInfo).GetValue(value, BindingFlags.GetProperty, null, eventArgs.IndexParameters, CultureInfo.InvariantCulture); } catch (Exception exception5) { value = null; eventArgs.Action = PathWalkAction.Cancel; if (!refActivity.DesignMode) { TargetInvocationException exception6 = exception5 as TargetInvocationException; throw (exception6 != null) ? exception6.InnerException : exception5; } } break; } if (((value is ActivityBind) && !eventArgs.LastMemberInThePath) && (GetMemberType(eventArgs.MemberInfo) != typeof(ActivityBind))) { while (value is ActivityBind) { ActivityBind bind = value as ActivityBind; if (recursionContext.Contains(refActivity, bind)) { throw new InvalidOperationException(SR.GetString("Bind_ActivityDataSourceRecursionDetected")); } recursionContext.Add(refActivity, bind); value = bind.GetRuntimeValue(refActivity); } } } }) }.TryWalkPropertyPath(refActivity.GetType(), path)) { return value; } return null; }
//This function is used to get the outermost activity bind which is bound to actual field/property //The function is called in OnRuntimeInitialized and makes sure that we get outer target bind and cache //it so that at runtime we do not have to walk the bind chain to find the bound member private static ActivityBind GetContextBind(ActivityBind activityBind, Activity activity, out Activity contextActivity) { if (activityBind == null) throw new ArgumentNullException("activityBind"); if (activity == null) throw new ArgumentNullException("activity"); BindRecursionContext recursionContext = new BindRecursionContext(); ActivityBind contextBind = activityBind; contextActivity = activity; while (contextBind != null) { Activity resolvedActivity = Helpers.ParseActivityForBind(contextActivity, contextBind.Name); if (resolvedActivity == null) return null; object dataSourceObject = resolvedActivity; MemberInfo memberInfo = ActivityBind.GetMemberInfo(dataSourceObject.GetType(), contextBind.Path, null); if (memberInfo == null) { contextActivity = resolvedActivity; return contextBind; } else if (memberInfo is FieldInfo) { contextActivity = resolvedActivity; return contextBind; } else if (memberInfo is PropertyInfo && (memberInfo as PropertyInfo).PropertyType == typeof(ActivityBind) && dataSourceObject != null) { object value = MemberBind.GetValue(memberInfo, dataSourceObject, contextBind.Path); if (value is ActivityBind) { if (recursionContext.Contains(contextActivity, contextBind)) return null; recursionContext.Add(contextActivity, contextBind); contextActivity = resolvedActivity; contextBind = value as ActivityBind; } else { return null; } } else { return null; } } return contextBind; }
internal static object ResolveActivityPath(Activity refActivity, string path) { if (refActivity == null) throw new ArgumentNullException("refActivity"); if (path == null) throw new ArgumentNullException("path"); if (path.Length == 0) throw new ArgumentException(SR.GetString(SR.Error_EmptyPathValue), "path"); object value = refActivity; BindRecursionContext recursionContext = new BindRecursionContext(); PathWalker pathWalker = new PathWalker(); pathWalker.MemberFound += delegate(object sender, PathMemberInfoEventArgs eventArgs) { // If value is null, we don't want to use GetValue on the MemberInfo if (value == null) { eventArgs.Action = PathWalkAction.Cancel; //need to cancel the walk with the failure return result return; } switch (eventArgs.MemberKind) { case PathMemberKind.Field: try { value = (eventArgs.MemberInfo as FieldInfo).GetValue(value); } catch (Exception exception) { //in some cases the value might not be there yet (e.g. validation vs. runtime) value = null; eventArgs.Action = PathWalkAction.Cancel; //we should throw only if we are at the runtime if (!refActivity.DesignMode) { TargetInvocationException targetInvocationException = exception as TargetInvocationException; throw (targetInvocationException != null) ? targetInvocationException.InnerException : exception; } } break; case PathMemberKind.Event: EventInfo evt = eventArgs.MemberInfo as EventInfo; // GetValue() returns the actual value of the property. We need the Bind object here. // Find out if there is a matching dependency property and get the value throw the DP. DependencyProperty eventDependencyProperty = DependencyProperty.FromName(evt.Name, value.GetType()); if (eventDependencyProperty != null && value is DependencyObject) { if ((value as DependencyObject).IsBindingSet(eventDependencyProperty)) value = (value as DependencyObject).GetBinding(eventDependencyProperty); else value = (value as DependencyObject).GetHandler(eventDependencyProperty); } break; case PathMemberKind.Property: if (!(eventArgs.MemberInfo as PropertyInfo).CanRead) { eventArgs.Action = PathWalkAction.Cancel; return; } // GetValue() returns the actual value of the property. We need the Bind object here. // Find out if there is a matching dependency property and get the value throw the DP. DependencyProperty dependencyProperty = DependencyProperty.FromName(eventArgs.MemberInfo.Name, value.GetType()); if (dependencyProperty != null && value is DependencyObject && (value as DependencyObject).IsBindingSet(dependencyProperty)) value = (value as DependencyObject).GetBinding(dependencyProperty); else try { value = (eventArgs.MemberInfo as PropertyInfo).GetValue(value, null); } catch (Exception exception) { //property getter function might throw at design time, validation should not fail bacause of that value = null; eventArgs.Action = PathWalkAction.Cancel; //we should throw only if we are at the runtime if (!refActivity.DesignMode) { TargetInvocationException targetInvocationException = exception as TargetInvocationException; throw (targetInvocationException != null) ? targetInvocationException.InnerException : exception; } } break; case PathMemberKind.IndexedProperty: case PathMemberKind.Index: try { value = (eventArgs.MemberInfo as PropertyInfo).GetValue(value, BindingFlags.GetProperty, null, eventArgs.IndexParameters, CultureInfo.InvariantCulture); } catch (Exception exception) { //in some cases the value might not be there yet - e.g. array or dictionary is populated at runtime only (validation vs. runtime) value = null; eventArgs.Action = PathWalkAction.Cancel; //we should throw only if we are at the runtime if (!refActivity.DesignMode) { TargetInvocationException targetInvocationException = exception as TargetInvocationException; throw (targetInvocationException != null) ? targetInvocationException.InnerException : exception; } } break; } //need to unwrap the activity bind if we get one - to proceed with the actual field/property/delegate //do not unwrap if the property/field is itself of type ActivityBind //we should not unwrap the latest ActivityBind though - only intermediate ones //avoid circular reference problems with the BindRecursionContext if (value is ActivityBind && !eventArgs.LastMemberInThePath && GetMemberType(eventArgs.MemberInfo) != typeof(ActivityBind)) { while (value is ActivityBind) { ActivityBind activityBind = value as ActivityBind; if (recursionContext.Contains(refActivity, activityBind)) throw new InvalidOperationException(SR.GetString(SR.Bind_ActivityDataSourceRecursionDetected)); recursionContext.Add(refActivity, activityBind); value = activityBind.GetRuntimeValue(refActivity); } } }; if (pathWalker.TryWalkPropertyPath(refActivity.GetType(), path)) return value; else return null; }
internal static object ResolveActivityPath(Activity refActivity, string path) { PathWalker walker; if (refActivity == null) { throw new ArgumentNullException("refActivity"); } if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException(SR.GetString("Error_EmptyPathValue"), "path"); } object value = refActivity; BindRecursionContext recursionContext = new BindRecursionContext(); if (new PathWalker { MemberFound = (EventHandler <PathMemberInfoEventArgs>) Delegate.Combine(walker.MemberFound, delegate(object sender, PathMemberInfoEventArgs eventArgs) { if (value == null) { eventArgs.Action = PathWalkAction.Cancel; } else { switch (eventArgs.MemberKind) { case PathMemberKind.Field: try { value = (eventArgs.MemberInfo as FieldInfo).GetValue(value); } catch (Exception exception) { value = null; eventArgs.Action = PathWalkAction.Cancel; if (!refActivity.DesignMode) { TargetInvocationException exception2 = exception as TargetInvocationException; throw (exception2 != null) ? exception2.InnerException : exception; } } break; case PathMemberKind.Event: { EventInfo memberInfo = eventArgs.MemberInfo as EventInfo; DependencyProperty dependencyProperty = DependencyProperty.FromName(memberInfo.Name, value.GetType()); if ((dependencyProperty != null) && (value is DependencyObject)) { if ((value as DependencyObject).IsBindingSet(dependencyProperty)) { value = (value as DependencyObject).GetBinding(dependencyProperty); } else { value = (value as DependencyObject).GetHandler(dependencyProperty); } } break; } case PathMemberKind.Property: if ((eventArgs.MemberInfo as PropertyInfo).CanRead) { DependencyProperty property2 = DependencyProperty.FromName(eventArgs.MemberInfo.Name, value.GetType()); if (((property2 != null) && (value is DependencyObject)) && (value as DependencyObject).IsBindingSet(property2)) { value = (value as DependencyObject).GetBinding(property2); } else { try { value = (eventArgs.MemberInfo as PropertyInfo).GetValue(value, null); } catch (Exception exception3) { value = null; eventArgs.Action = PathWalkAction.Cancel; if (!refActivity.DesignMode) { TargetInvocationException exception4 = exception3 as TargetInvocationException; throw (exception4 != null) ? exception4.InnerException : exception3; } } } break; } eventArgs.Action = PathWalkAction.Cancel; return; case PathMemberKind.IndexedProperty: case PathMemberKind.Index: try { value = (eventArgs.MemberInfo as PropertyInfo).GetValue(value, BindingFlags.GetProperty, null, eventArgs.IndexParameters, CultureInfo.InvariantCulture); } catch (Exception exception5) { value = null; eventArgs.Action = PathWalkAction.Cancel; if (!refActivity.DesignMode) { TargetInvocationException exception6 = exception5 as TargetInvocationException; throw (exception6 != null) ? exception6.InnerException : exception5; } } break; } if (((value is ActivityBind) && !eventArgs.LastMemberInThePath) && (GetMemberType(eventArgs.MemberInfo) != typeof(ActivityBind))) { while (value is ActivityBind) { ActivityBind bind = value as ActivityBind; if (recursionContext.Contains(refActivity, bind)) { throw new InvalidOperationException(SR.GetString("Bind_ActivityDataSourceRecursionDetected")); } recursionContext.Add(refActivity, bind); value = bind.GetRuntimeValue(refActivity); } } } }) }.TryWalkPropertyPath(refActivity.GetType(), path)) { return(value); } return(null); }
private static ActivityBind GetContextBind(ActivityBind activityBind, Activity activity, out Activity contextActivity) { if (activityBind == null) { throw new ArgumentNullException("activityBind"); } if (activity == null) { throw new ArgumentNullException("activity"); } BindRecursionContext context = new BindRecursionContext(); ActivityBind bind = activityBind; contextActivity = activity; while (bind != null) { Activity activity2 = Helpers.ParseActivityForBind(contextActivity, bind.Name); if (activity2 == null) { return null; } object dataContext = activity2; MemberInfo memberInfo = GetMemberInfo(dataContext.GetType(), bind.Path, null); if (memberInfo == null) { contextActivity = activity2; return bind; } if (memberInfo is FieldInfo) { contextActivity = activity2; return bind; } if ((!(memberInfo is PropertyInfo) || !((memberInfo as PropertyInfo).PropertyType == typeof(ActivityBind))) || (dataContext == null)) { return null; } object obj3 = MemberBind.GetValue(memberInfo, dataContext, bind.Path); if (!(obj3 is ActivityBind)) { return null; } if (context.Contains(contextActivity, bind)) { return null; } context.Add(contextActivity, bind); contextActivity = activity2; bind = obj3 as ActivityBind; } return bind; }