namespace IvyBus { using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Threading; using System.Text.RegularExpressions; using System.Collections.ObjectModel; /* This is the Application side of binding storage */ /* association of a generated Key and a delegate and the expression */ /* this is SEND to other client */ public class IvyApplicationBinding { private BindingType binding; // Argument formatting Expression to replace argument in in regular expresion binding // format %number[:objectFormat]% // where number in argument index and objectFormat the ToString format passed to argument private Regex re = new Regex(@"%(\d+)(|:(.*))%"); public BindingType Binding { get { return binding; } set { binding = value; } } private int key; public int Key { get { return key; } set { key = value; } } private object[] args; private string expression; public string Expression { get { return expression; } set { expression = value; } } private string formatted_expression; public string FormattedExpression { get { FormatExpression(); return formatted_expression; } } private Collection arguments; ///SentMessageClasses the first word token of sent messages /// optimise the parsing process when sending messages /// public Collection Arguments { get { return arguments; } } public event EventHandler Callback; public IvyApplicationBinding() :this(BindingType.RegularExpression,string.Empty,null) { } internal IvyApplicationBinding(BindingType type, string expression, params object[] args) { arguments = new Collection(); this.Binding = type; this.Expression = expression; this.args = args; } private string ReplaceArgs(Match m) { string s = string.Empty; // Get the matched string. convert to argindex int argindex = int.Parse(m.Groups[1].Value); // use %... to specify formating object arg = args[argindex]; IFormattable fmt = arg as IFormattable; if (fmt != null) s = fmt.ToString(m.Groups[3].Value, null); else if (arg != null) s = arg.ToString(); return s; } // translate part of expression to object property public void FormatExpression() { //// Safely : #if (!PocketPC)//TODO Pocket PC doesn't have Target Member EventHandler temp = Callback; if (temp != null) { //TODO Pocket PC doesn't have Target Member object target = temp.Target; if (args == null) { args = new object[arguments.Count]; for (int i = 0; i < arguments.Count; i++) { System.Reflection.PropertyInfo prop = target.GetType().GetProperty(arguments[i]); if (prop != null) args[i] = prop.GetValue(target, null); else //TODO what else BUG msgbox in desing mode !!! args[i] = arguments[i]; } } formatted_expression = re.Replace(expression, ReplaceArgs); } else //TODO Abnormal condition Design Time #endif formatted_expression = expression; } #if ( PocketPC ) internal void Firevent(System.Windows.Forms.Control control, IvyMessageEventArgs e) { //// Safely invoke an event: EventHandler temp = Callback; if (temp == null) { throw new IvyException("(callCallback) Not callback for id " + e.Id); } if (control != null) { control.Invoke(temp, this, e); } else temp(this, e); } #else internal void Firevent(System.Threading.SynchronizationContext syncContext, bool synchronous , IvyMessageEventArgs e) { //// Safely invoke an event: EventHandler temp = Callback; if (temp == null) { throw new IvyException("(callCallback) Not callback for id " + e.Id); } if (synchronous ) { SendOrPostCallback update = delegate(object state) { IvyMessageEventArgs args = (IvyMessageEventArgs)state; temp(this, args); }; syncContext.Post(update, e); } else temp(this, e); } #endif } }