/// <summary> /// Gets property info based on property expression. /// </summary> /// <typeparam name="TEntity">Entity type.</typeparam> /// <typeparam name="TProperty">Member type.</typeparam> /// <param name="memberExpression">Member expression.</param> /// <returns>Member info.</returns> public static MemberInfo GetMemberInfo <TEntity, TProperty>(Expression <Func <TEntity, TProperty> > memberExpression) { ParametersValidator.IsNotNull(memberExpression, () => memberExpression); var memberAccess = (MemberExpression)memberExpression.Body; return(memberAccess.Member); }
/// <summary> /// .ctor. Try to acquire lock with given timeout. /// <exception cref="InvalidOperationException"> If wrong SlimLockMode specified</exception> /// <exception cref="ApplicationException"> If lock cannot be acquired during timeout</exception> /// <exception cref="ArgumentNullException"> alock is null</exception> /// <exception cref="LockRecursionException"> /// <see cref="ReaderWriterLockSlim.TryEnterReadLock(System.TimeSpan)"/> /// <see cref="ReaderWriterLockSlim.TryEnterUpgradeableReadLock(System.TimeSpan)"/> /// <see cref="ReaderWriterLockSlim.TryEnterWriteLock(System.TimeSpan)"/> /// </exception> /// </summary> /// <param name="alock">Lock object</param> /// <param name="mode">Mode to acquire lock</param> /// <param name="timeout">Timeout to acquire lock</param> public DisposableReaderWriterLockSlim(ReaderWriterLockSlim alock, TimeSpan timeout, SlimLockMode mode = SlimLockMode.Read) { ParametersValidator.IsNotNull(alock, () => alock); _lock = alock; _mode = mode; _timeout = timeout; AcquireLock(); Logger.TraceFormat("lock created timeout={0}, mode ={1}", timeout, mode); }
/// <summary> /// Gets Where expression by object type, property name and selector value. /// </summary> /// <typeparam name="T">Object type.</typeparam> /// <param name="property">Property name.</param> /// <param name="value">Object value.</param> /// <returns></returns> public static Expression <Func <T, bool> > GetWhereEqualExpression <T>(string property, object value) where T : class { ParametersValidator.IsNotNullOrWhiteSpace(property, () => property); ParametersValidator.IsNotNull(value, () => value); var propertyInfo = typeof(T).GetProperty(property); return(GetWhereEqualExpression <T>(propertyInfo, value)); }
/// <summary> /// Wraps action into try/finally statement with getting a write lock in try and releasing in finally. /// Default timeout of <see cref="DisposableReaderWriterLockSlim"/> is used. /// </summary> /// <param name="alock">Lock to handle</param> /// <param name="action">Action to execute</param> public static void WithWriteLock(this ReaderWriterLockSlim alock, Action action) { ParametersValidator.IsNotNull(alock, () => alock); ParametersValidator.IsNotNull(action, () => action); Logger.TraceFormat("Runnig action {0} with slim lock {1} with write lock", action, alock); using (new DisposableReaderWriterLockSlim(alock, SlimLockMode.Write)) { action(); } }
/// <summary> /// Wraps action into try/finally statement with getting a read lock in try and releasing in finally. /// </summary> /// <param name="alock">Lock to handle</param> /// <param name="action">Action to execute</param> /// <param name="timeout">Timeout to get lock</param> public static void WithReadLock(this ReaderWriterLockSlim alock, Action action, TimeSpan timeout) { ParametersValidator.IsNotNull(alock, () => alock); ParametersValidator.IsNotNull(action, () => action); Logger.TraceFormat("Runnig action {0} with slim lock {1} and timeout {2} with read lock", action, alock, timeout); using (new DisposableReaderWriterLockSlim(alock, timeout)) { action(); } }
/// <summary> /// Wraps any action into a using statement /// </summary> /// <param name="d">Disposable to handle</param> /// <param name="action">Action to execute</param> public static void RunSafe(this IDisposable d, Action action) { ParametersValidator.IsNotNull(d, () => d); ParametersValidator.IsNotNull(action, () => action); Logger.TraceFormat("Runnig action {0} with disposable {1}", action, d); using (d) { action(); } }
/// <summary> /// Wraps action into try/finally statement with getting a write lock in try and releasing in finally. /// Default timeout of <see cref="DisposableReaderWriterLockSlim"/> is used. /// </summary> /// <param name="alock">Lock to handle</param> /// <param name="action">Action to execute</param> /// <param name="timeout">TImeout to get write lock</param> public static void WithWriteLock(this ReaderWriterLock alock, Action action, TimeSpan timeout) { ParametersValidator.IsNotNull(alock, () => alock); ParametersValidator.IsNotNull(action, () => action); Logger.TraceFormat("Runnig action {0} with lock {1} with write lock and timeout {2}", action, alock, timeout); using (new DisposableReaderWriterLock(alock, timeout, LockMode.Write)) { action(); } }
/// <summary> /// Wraps action into try/finally statement with getting a read lock in try and releasing in finally. /// Default timeout of <see cref="DisposableReaderWriterLockSlim"/> is used. /// </summary> /// <param name="alock">Lock to handle</param> /// <param name="action">Action to execute</param> public static void WithReadLock(this ReaderWriterLock alock, Action action) { ParametersValidator.IsNotNull(alock, () => alock); ParametersValidator.IsNotNull(action, () => action); Logger.TraceFormat("Runnig action {0} with lock {1} with read lock", action, alock); using (new DisposableReaderWriterLock(alock)) { action(); } }
/// <summary> /// Joins two expressions with logical Or. /// </summary> /// <typeparam name="T">Object type.</typeparam> /// <param name="first">First expression.</param> /// <param name="second">Second expression.</param> /// <returns>Join of two expressions with logical And.</returns> public static Expression <Func <T, bool> > Or <T>(Expression <Func <T, bool> > first, Expression <Func <T, bool> > second) { ParametersValidator.IsNotNull(first, () => first); ParametersValidator.IsNotNull(second, () => second); var invokedExpr = System.Linq.Expressions.Expression.Invoke(second, first.Parameters); var andExpression = System.Linq.Expressions.Expression.OrElse(first.Body, invokedExpr); var result = System.Linq.Expressions.Expression.Lambda <Func <T, bool> >(andExpression, first.Parameters[0]); return(result); }
/// <summary> /// Gets Where expression by object type, property info and selector value. /// </summary> /// <typeparam name="T">Object type.</typeparam> /// <param name="property">Property info.</param> /// <param name="value">Object value.</param> /// <returns></returns> public static Expression <Func <T, bool> > GetWhereEqualExpression <T>(PropertyInfo property, object value) where T : class { ParametersValidator.IsNotNull(property, () => property); ParametersValidator.IsNotNull(value, () => value); var parameter = System.Linq.Expressions.Expression.Parameter(typeof(T), "x"); var propertyExpression = System.Linq.Expressions.Expression.MakeMemberAccess(parameter, property); var constExpression = System.Linq.Expressions.Expression.Constant(value, value.GetType()); var body = System.Linq.Expressions.Expression.Equal(propertyExpression, constExpression); var result = System.Linq.Expressions.Expression.Lambda <Func <T, bool> >(body, parameter); return(result); }
public IDictionary <string, string> Parse(string commandLine) { ParametersValidator.IsNotNull(commandLine, () => commandLine); Logger.DebugFormat("Parsing command line {0}", commandLine); var result = new Dictionary <string, string>(); var re = new Regex(@"(?<switch> -{1,2}\S*)(?:[=:]?|\s+)(?<value> [^-\s].*?)?(?=\s+[-\/]|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase); var matches = re.Matches(commandLine); foreach (Match m in matches) { var sw = m.Groups["switch"].Value; sw = sw.TrimStart(' ', '-'); var val = m.Groups["value"].Value; val = val.Trim(); result[sw] = val; } Logger.DebugFormat("Parsed command line {0}, {1} matches", commandLine, result.Count); return(result); }
public void OneParamIsNotNullThrowTest() { object obj = null; ParametersValidator.IsNotNull(() => obj); }
public void OneParamIsNotNullPassTest() { var obj = new object(); ParametersValidator.IsNotNull(() => obj); }
/// <summary> /// Gets member name based on property expression. /// </summary> /// <typeparam name="TEntity">Entity type.</typeparam> /// <typeparam name="TProperty">Member type.</typeparam> /// <param name="memberExpression">Member expression.</param> /// <returns>Member name.</returns> public static string GetMemberName <TEntity, TProperty>(Expression <Func <TEntity, TProperty> > memberExpression) { ParametersValidator.IsNotNull(memberExpression, () => memberExpression); return(GetMemberInfo(memberExpression).Name); }
/// <summary> /// .ctor /// </summary> /// <param name="items">Array of dispoables </param> public CompositeDisposable(IReadOnlyCollection <IDisposable> items) { ParametersValidator.IsNotNull(items, () => items); _lst = new List <IDisposable>(items); Logger.TraceFormat("Composite disposable created with {0} childs", _lst.Count); }