Esempio n. 1
0
        /// <summary>
        /// 检查输入的条件参数 <paramref name="condition"/> 函数运算结果是否为真(true)。
        /// 如果条件参数 <paramref name="condition"/> 函数运算结果值为 false,则抛出 <see cref="System.InvalidOperationException"/> 异常;否则返回 true。
        /// </summary>
        /// <param name="condition">被检查的条件参数,该参数是一个运算结果为 System.Nullable&gt;bool?&lt; 类型值的委托。</param>
        /// <param name="message">当被检查的条件参数 <paramref name="condition"/> 函数运算结果不为真(true)时,抛出的 <see cref="System.InvalidOperationException"/> 异常中所带的消息。</param>
        /// <param name="abortOnFailed">指定在指定函数 <paramref name="condition"/> 时,是否屏蔽其执行过程中可能会抛出的异常。</param>
        /// <returns>如果条件参数 <paramref name="condition"/> 运算结果为真(true),则返回 true。</returns>
        /// <exception cref="System.ArgumentNullException">如果被检查的条件参数为 Null 时,则抛出该异常。</exception>
        /// <exception cref="System.InvalidOperationException">如果被检查的条件参数 <paramref name="condition"/> 运算结果不为真(true),则抛出该异常。</exception>
        public static bool NotTrue(Func <bool?> condition, string message, bool abortOnFailed = false)
        {
            Check.NotNull(condition);
            bool?b   = abortOnFailed ? condition() : Trying.Try(condition);
            bool ret = b.HasValue ? b.HasValue : false;

            return(NotTrue(ret, message));
        }
        /// <summary>
        /// 将当前对象中所有属性的值按照属性名称和类型定义的匹配关系复制到另一对象中。
        /// </summary>
        /// <param name="_this">
        /// 表示将要用于复制数据到另一对象的元数据对象。
        /// 如果该参数值为 Null,将不会执行复制操作。
        /// </param>
        /// <param name="targetElement">表示一个目标对象,该对象中的相应属性值将会被更改。</param>
        /// <param name="abortOnFailed">一个布尔类型值,该值指示在复制数据过程中如果出现异常,是否立即停止并抛出异常,默认为 false。</param>
        public static void CopyTo(this object _this, object targetElement, bool abortOnFailed = false)
        {
            if (_this == null)
            {
                return;
            }

            Check.NotNull(targetElement);
            Type thisType = _this.GetType(), elemType = targetElement.GetType();

            var thisProps = thisType.GetProperties().Where(p => p.GetMethod != null);
            var elemProps = elemType.GetProperties().Where(p => p.SetMethod != null);

            foreach (PropertyInfo thisProperty in thisProps)
            {
                PropertyInfo elemProperty = elemProps.FirstOrDefault(p => p.Name == thisProperty.Name);
                if (elemProperty != null && elemProperty.PropertyType.IsAssignableFrom(thisProperty.PropertyType))
                {
                    if (abortOnFailed)
                    {
                        elemProperty.SetValue(targetElement, thisProperty.GetValue(_this));
                    }
                    else
                    {
                        Trying.Try(() => elemProperty.SetValue(targetElement, thisProperty.GetValue(_this)));
                    }
                }
            }

            var thisFields = thisType.GetFields();
            var elemFields = elemType.GetFields();

            foreach (FieldInfo thisField in thisFields)
            {
                FieldInfo elemField = elemFields.FirstOrDefault(f => f.Name == thisField.Name);
                if (elemField != null && elemField.FieldType.IsAssignableFrom(thisField.FieldType))
                {
                    if (abortOnFailed)
                    {
                        elemField.SetValue(targetElement, thisField.GetValue(_this));
                    }
                    else
                    {
                        Trying.Try(() => elemField.SetValue(targetElement, thisField.GetValue(_this)));
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 将一个数据行对象转换成一个指定类型的 CLR 对象。
        /// <paramref name="row"/> 数据行对象中的所有数据项将作为属性值反映在返回的 CLR 对象中。
        /// 对于不能转换的数据项(例如指定的 <typeparamref name="TResult"/> 类型中没有该数据项所示名称的属性、或者数据类型不匹配),将不会包含在返回的 CLR 对象中。
        /// </summary>
        /// <typeparam name="TResult">指定的 CLR 类型。</typeparam>
        /// <param name="row"></param>
        /// <returns></returns>
        public static TResult ToObject <TResult>(this DataRow row) where TResult : class, new()
        {
            Check.NotNull(row);

            Type    type = typeof(TResult);
            TResult ret  = new TResult();
            IEnumerable <PropertyInfo> properties = type.GetProperties().Where(p => p.SetMethod != null);

            foreach (string name in row.Table.GetColumnNames())
            {
                PropertyInfo property = properties.FirstOrDefault(p => p.Name == name);
                if (property == null)
                {
                    continue;
                }

                object value = row[name];
                if (value.IsNull() && !property.PropertyType.IsValueType)
                {
                    Trying.Try(() => property.SetValue(ret, property.PropertyType == typeof(DBNull) ? DBNull.Value : value));
                }
                else
                {
                    if (property.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        Trying.Try(() => property.SetValue(ret, value));
                    }
                    else
                    {
                        if (value is IConvertible && property.PropertyType.IsImplementOf(typeof(IConvertible)))
                        {
                            object val = Convert.ChangeType(value, property.PropertyType);
                            Trying.Try(() => property.SetValue(ret, val));
                        }
                    }
                }
            }
            return(ret);
        }
        public static void NotTrue(Func <bool?> condition, string message, string detailMessage, bool abortOnFailed = false, params object[] args)
        {
            bool?b = abortOnFailed ? condition() : Trying.Try(condition);

            Debug.Assert(b.HasValue ? b.Value : false, message, detailMessage, args);
        }
        public static void NotTrue(Func <bool?> condition, string message, bool abortOnFailed = false)
        {
            bool?b = abortOnFailed ? condition() : Trying.Try(condition);

            Debug.Assert(b.HasValue ? b.Value : false, message);
        }