public static void Demand([AssertionCondition(AssertionConditionType.IS_TRUE)] bool condition, Func <string> message, object sender, string uniqueId) { if (condition) { return; } string msg = null; if (message == null) { msg = "A requirement was examined and declined." + DebugUtil.P("Method", new StackFrame(1).GetMethod().Name); } else { msg = message(); } msg += DebugUtil.P(sender != null, "Demanding type", DebugUtil.FormatTypeName(sender)) + DebugUtil.P(uniqueId != null, "UniqueID", uniqueId); /*DEBUG*/ if (Debugger.IsAttached) { Debug.WriteLine(message); Debugger.Break(); } throw new InvalidOperationException(msg); }
public static void DemandWriteOnce([AssertionCondition(AssertionConditionType.IS_TRUE)] bool canWrite, string message, object owner, string propertyName, string uniqueId) { //NOTE: do not change default parameter values! if (!canWrite) { throw new InvalidOperationException( (message == null ? "Property already initialized!": DebugUtil.Indent2(message)) + DebugUtil.P(owner != null, "Declaring Type", DebugUtil.FormatTypeName(owner)) + DebugUtil.P(propertyName != null, "Property Name", propertyName) + DebugUtil.P(uniqueId != null, "UniqueID", uniqueId) ); } }
/// <summary> Helps to safely raise an event. /// </summary> /// <param name="delegate">The event delegate (<see cref="EventHandler"/>, <see cref="EventHandler{T}"/></param> /// <param name="sender">The sender</param> /// <param name="e">The event arguments</param> /// <param name="uniqueId">A unique ID</param> /// <exception cref="MultiTargetInvocationException"></exception> public static void Raise(Delegate @delegate, object sender, EventArgs e, string uniqueId) { if (@delegate == null) { return; } #region simple invoke (conditional) #if (false) // simple invoke the delegate w/o any other stuff (DynamicInvoke is slow!) try{ @delegate.DynamicInvoke(new[] { sender, e }); } catch (Exception ex) { if (ObjectVM.IsInDesignMode) { return; // ignore designtime error } throw; // possible breakpoint for debug } return; #endif #endregion // var exceptions=new List<Exception>(); var exceptions = (List <Exception>)null; //don't collect, do throw directly var invocationList = @delegate.GetInvocationList(); #region Trace (conditional) #if (false) Debug.WriteLine("=>Raise event: " + "(" + invocationList.Length + " target" + (invocationList.Length != 1?"s":"") + ")"); foreach (var d in invocationList) { var targetType = d.Target.GetType().FullName; var method = d.Method.ToString(); Debug.WriteLine("=>\t" + "Target: " + targetType + " " + method); } Debug.WriteLine("=>\t" + "Raising method: " + sender.GetType().FullName + "." + new StackFrame(1).GetMethod()); // MemberPath (ObjectVM/ObjectBM) var memberPathProperty = sender.GetType().GetProperty("MemberPath"); if (memberPathProperty != null) { var memberPath = memberPathProperty.GetValue(sender, null); Debug.WriteLine("=>\t" + "MemberPath: " + memberPath); } #endif #endregion var isInvokeRequired = ApplicationDispatcher.IsInvokeRequired; foreach (var d in invocationList) { #region DEBUG (conditional) #if (false) Debug.WriteLine(string.Format("=>Raise event: #{0} {1} {2}", ++InvocationCount, DebugUtil.FormatTypeName(@delegate), "Target: " + DebugUtil.FormatTypeName(d.Target) + "." + d.Method.Name )); #endif #endregion // EXPERIMENTAL ==> // workaround for TargetInvocationException --> InvalidOperationException: // "The calling thread cannot access this object because a different thread owns it." // e.g. System.Windows.Input.CanExecuteChangedEventManager+HandlerSink, PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 if (isInvokeRequired) { if (d.Target.GetType().FullName == "System.Windows.Input.CanExecuteChangedEventManager+HandlerSink") { InvokeAppDispatcher(d, sender, e, exceptions); } else if (d.Target.GetType().FullName == "System.Windows.Data.ListCollectionView") { InvokeAppDispatcher(d, sender, e, exceptions); } /*??*/ else if (d.Target.GetType().Assembly.GetName().Name == "PresentationCore") { InvokeAppDispatcher(d, sender, e, exceptions); } else { Invoke(d, sender, e, exceptions); } } //<== else { Invoke(d, sender, e, exceptions); } } if (exceptions != null && exceptions.Count > 0) { var ex = new MultiTargetInvocationException(exceptions); #region DESIGNER if (IsInDesignMode) { // Trace.Write("=>Begin:" + exceptionCount + "######################################################################################################################################"+"\n"); Trace.Write("=>An unhandled exception has occured. Trying to continue." + "\n" + ex.ToString() + "\n"); foreach (var exception in exceptions) { Trace.Write("--------------------------------------------------------------------------------------------------------------------------------------" + "\n" + exception.ToString() + "\n"); } // Trace.Write("=>End :" + exceptionCount + "######################################################################################################################################"+"\n"); return; } #endregion throw ex; } }