通过反射设置属性名为_propertyName的值|如需要性能提高,可以override
2017-10-14 21:02:29 访问(1519) 赞(0) 踩(0)
#region 通过反射设置属性名为_propertyName的值|如需要性能提高,可以override
/// <summary>
/// 设置属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <param name="theValue">变量值</param>
public virtual void SetPropertyValue(string _propertyName, object theValue)
{
if (_propertyName == null || _propertyName.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:string _propertyName为null。"
);
}
Type thisType = this.GetType();
PropertyInfo info = thisType.GetProperty(_propertyName, BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance);
if (info == null)
{
string lowerPropertyName = _propertyName.Trim().ToLower();
switch (lowerPropertyName)
{
case "p__sqlrownumbervalue":
// 默认的分页页码值 //
// 返回 //
// oracle 算法分页中会用到,不是model的属性值,所以忽略 //
return;
default:
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "PropertyInfo info = thisType.GetProperty(_propertyName[" + _propertyName + "]);"
);
}
}
if (theValue == DBNull.Value)
theValue = null;
info.SetValue(this, theValue, null);
}
/// <summary>
/// 通过反射设置属性名为_propertyName的值|如需要性能提高,可以override
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <param name="isThrowException">是否抛出异常</param>
/// <param name="theValue">变量值</param>
public virtual void SetPropertyValue(string _propertyName, bool isThrowException, object theValue)
{
if (_propertyName == null || _propertyName.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:string _propertyName为null。"
);
}
Type thisType = this.GetType();
PropertyInfo info = thisType.GetProperty(_propertyName, BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance);
if (info == null)
{
if (isThrowException)
{
string lowerPropertyName = _propertyName.Trim().ToLower();
switch (lowerPropertyName)
{
case "p__sqlrownumbervalue":
// 默认的分页页码值 //
// 返回 //
// oracle 算法分页中会用到,不是model的属性值,所以忽略 //
return;
default:
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "PropertyInfo info = thisType.GetProperty(_propertyName[" + _propertyName + "]);"
);
}
}
else
return;
}
if (theValue == DBNull.Value || theValue == null)
{
info.SetValue(this, null, null);
}
else
{
if (info.PropertyType == typeBoolean
||
info.PropertyType == typeBooleanNull)
{
if (theValue is Boolean)
info.SetValue(this, theValue, null);
else
info.SetValue(this, (theValue.ToString() == "1"), null);
}
else
{
info.SetValue(this, theValue, null);
}
}
}
#endregion 通过反射设置属性名为_propertyName的值|如需要性能提高,可以override
上一条:
下一条:
相关评论
发表评论