public static Subscription ByExample <TMessage>(Expression <Func <Builder, TMessage> > factory) where TMessage : IMessage { if (factory.Body.NodeType != ExpressionType.New) { throw new ArgumentException(); } var parameterValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); using (CultureScope.Invariant()) { var newExpression = (NewExpression)factory.Body; var parameters = newExpression.Constructor.GetParameters(); for (var argumentIndex = 0; argumentIndex < newExpression.Arguments.Count; ++argumentIndex) { var argumentExpression = newExpression.Arguments[argumentIndex]; var parameterName = parameters[argumentIndex].Name; var parameterValue = GetExpressionValue(argumentExpression); if (parameterValue != null) { parameterValues[parameterName] = parameterValue.ToString(); } } } return(new Subscription(MessageUtil.TypeId <TMessage>(), BindingKey.Create(typeof(TMessage), parameterValues))); }
public void should_set_invariant_culture_and_reset_previous_culture_on_dispose() { var currentCulture = Thread.CurrentThread.CurrentCulture; var currentUICulture = Thread.CurrentThread.CurrentUICulture; using (CultureScope.Invariant()) { Thread.CurrentThread.CurrentCulture.ShouldEqual(CultureInfo.InvariantCulture); Thread.CurrentThread.CurrentUICulture.ShouldEqual(CultureInfo.InvariantCulture); } Thread.CurrentThread.CurrentCulture.ShouldEqual(currentCulture); Thread.CurrentThread.CurrentUICulture.ShouldEqual(currentUICulture); }
public static Subscription Matching <TMessage>(Expression <Func <TMessage, bool> > predicate) where TMessage : IMessage { var fieldValues = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); using (CultureScope.Invariant()) { var current = predicate.Body; while (current.NodeType == ExpressionType.And || current.NodeType == ExpressionType.AndAlso) { var binaryExpression = (BinaryExpression)current; AddFieldValue(fieldValues, (BinaryExpression)binaryExpression.Right); current = binaryExpression.Left; } AddFieldValue(fieldValues, (BinaryExpression)current); } return(new Subscription(MessageUtil.TypeId <TMessage>(), BindingKey.Create(typeof(TMessage), fieldValues))); }