Beispiel #1
0
        /// <summary> 尝试设置对象的属性/字段值,失败返回false
        /// </summary>
        /// <param name="instance">将要获取其属性/字段值的对象</param>
        /// <param name="value">成功将值保存在value,失败返回null</param>
        public bool TrySetValue(object instance, object value)
        {
            if (!CanWrite)
            {
                return(false);
            }
            if (instance == null)
            {
                if (Static == false)
                {
                    return(false);
                }
            }
            else if (ClassType.IsInstanceOfType(instance) == false)
            {
                return(false);
            }
            else if ((OriginalType.IsClass || Nullable) && (value == null || value is DBNull))
            {
                Setter(instance, null);
                return(true);
            }

            try {
                if (MemberType.IsInstanceOfType(value) == false)
                {
                    value = Convert.ChangeType(value, MemberType);
                }
                Setter(instance, value);
                return(true);
            } catch {
                return(false);
            }
        }
Beispiel #2
0
 /// <summary> 设置对象的属性/字段值
 /// </summary>
 /// <param name="instance">将要获取其属性/字段值的实例对象</param>
 /// <param name="value">将要设置的值</param>
 /// <exception cref="ArgumentNullException">实例属性instance对象不能为null</exception>
 public void SetValue(object instance, object value)
 {
     if (!CanWrite)
     {
         ErrorSetter(null, null);
     }
     else if (instance == null)
     {
         if (Static == false)
         {
             throw new ArgumentNullException("instance", "实例属性对象不能为null");
         }
     }
     else if ((value == null || value is DBNull) && (OriginalType.IsClass || Nullable))
     {
         Setter(instance, null);
         return;
     }
     if (MemberType.IsEnum)
     {
         var str = value as string;
         if (str != null)
         {
             value = Enum.Parse(MemberType, str, true);
         }
     }
     else if (MemberType.IsInstanceOfType(value) == false)
     {
         value = Convert.ChangeType(value, MemberType);
     }
     Setter(instance, value);
 }
Beispiel #3
0
 /// <summary> 尝试设置对象的属性/字段值,失败返回false
 /// </summary>
 /// <param name="instance">将要获取其属性/字段值的对象</param>
 /// <param name="value">成功将值保存在value,失败返回null</param>
 public bool TrySetValue(object instance, object value)
 {
     if (!CanWrite)
     {
         return(false);
     }
     else if (instance == null && Static == false)
     {
         return(false);
     }
     else if (ClassType.IsInstanceOfType(instance) == false)
     {
         return(false);
     }
     else if (MemberType.IsInstanceOfType(value) == false)
     {
         return(false);
     }
     try
     {
         Setter(instance, value);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Beispiel #4
0
 /// <summary> 设置对象的属性/字段值
 /// </summary>
 /// <param name="instance">将要获取其属性/字段值的实例对象</param>
 /// <param name="value">将要设置的值</param>
 public void SetValue(object instance, object value)
 {
     if (!CanWrite)
     {
         ErrorSetter(null, null);
     }
     else if (instance == null && Static == false)
     {
         throw new Exception("实例对象不能为null");
     }
     if (MemberType.IsInstanceOfType(value) == false)
     {
         value = Convert.ChangeType(value, MemberType);
     }
     Setter(instance, value);
 }
Beispiel #5
0
        /// <summary> 尝试设置对象的属性/字段值,失败返回false
        /// </summary>
        /// <param name="instance">将要获取其属性/字段值的对象</param>
        /// <param name="value">成功将值保存在value,失败返回null</param>
        public bool TrySetValue(object instance, object value)
        {
            if (!CanWrite)
            {
                return(false);
            }
            if (instance == null)
            {
                if (Static == false)
                {
                    return(false);
                }
            }
            else if (ClassType.IsInstanceOfType(instance) == false)
            {
                return(false);
            }
            else if ((value == null || value is DBNull) && (OriginalType.IsClass || Nullable))
            {
                Setter(instance, null);
                return(true);
            }

            try
            {
                if (MemberType.IsEnum)
                {
                    var str = value as string;
                    if (str != null)
                    {
                        value = Enum.Parse(MemberType, str, true);
                    }
                }
                else if (MemberType.IsInstanceOfType(value) == false)
                {
                    value = Convert.ChangeType(value, MemberType);
                }
                Setter(instance, value);
                return(true);
            }
            catch (Exception ex)
            {
                var message = $"{MemberInfo.ReflectedType.ToString()}.{Name}属性取值失败";
                Trace.WriteLine(ex, message);
                return(false);
            }
        }
Beispiel #6
0
 /// <summary> 设置对象的属性/字段值
 /// </summary>
 /// <param name="instance">将要获取其属性/字段值的实例对象</param>
 /// <param name="value">将要设置的值</param>
 /// <exception cref="ArgumentNullException">实例属性instance对象不能为null</exception>
 public void SetValue(object instance, object value)
 {
     if (!CanWrite)
     {
         ErrorSetter(null, null);
     }
     else if (instance == null)
     {
         if (Static == false)
         {
             throw new ArgumentNullException("instance", "实例属性对象不能为null");
         }
     }
     else if ((value == null || value is DBNull) && (OriginalType.IsClass || Nullable))
     {
         Setter(instance, null);
         return;
     }
     if (MemberType.IsEnum)
     {
         var str = value as string;
         if (str != null)
         {
             value = Enum.Parse(MemberType, str, true);
         }
         else
         {
             value = Enum.ToObject(MemberType, value);
         }
     }
     else if (MemberType.IsInstanceOfType(value) == false)
     {
         value = Convert.ChangeType(value, MemberType);
     }
     try
     {
         Setter(instance, value);
     }
     catch (Exception ex)
     {
         var message = $"{MemberInfo.ReflectedType.ToString()}.{Name}赋值失败";
         Trace.WriteLine(ex, message);
         throw new TargetInvocationException(message + ",原因见内部异常", ex);
     }
 }
Beispiel #7
0
 /// <summary> 设置对象的属性/字段值
 /// </summary>
 /// <param name="instance">将要获取其属性/字段值的实例对象</param>
 /// <param name="value">将要设置的值</param>
 /// <exception cref="ArgumentNullException">实例属性instance对象不能为null</exception>
 public void SetValue(object instance, object value)
 {
     if (!CanRead)
     {
         ErrorSetter(null, null);
     }
     else if (instance == null)
     {
         if (Static == false)
         {
             throw new ArgumentNullException("instance", "实例属性对象不能为null");
         }
     }
     else if ((OriginalType.IsClass || Nullable) && (value == null || value is DBNull))
     {
         Setter(instance, null);
         return;
     }
     if (MemberType.IsInstanceOfType(value) == false)
     {
         value = Convert.ChangeType(value, MemberType);
     }
     Setter(instance, value);
 }