BaseTreeModel
2017-10-14 21:39:27 访问(1791) 赞(0) 踩(0)
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace SlowX.CoreModel.TreeModel
{
/// <summary>
/// 这里存放表字段的实体值
/// </summary>
[Serializable]
public class BaseTreeModel
{
#region boolean 的 Type
/// <summary>
/// boolean 的 Type
/// </summary>
public readonly static Type typeBoolean = typeof(bool);
/// <summary>
/// 获得BooleanNull的Type
/// </summary>
public readonly static Type typeBooleanNull = typeof(bool?);
#endregion boolean 的 Type
#region 父节点
/// <summary>
/// 父节点|NULL代表根
/// </summary>
protected BaseTreeModel vm_Parent = null;
/// <summary>
/// 获得父节点|NULL代表根
/// </summary>
/// <returns></returns>
public BaseTreeModel ParentGet()
{
return vm_Parent;
}
/// <summary>
/// 设置父节点|NULL代表根
/// </summary>
/// <param name="theValue"></param>
public void ParentSet(BaseTreeModel theValue)
{
vm_Parent = theValue;
}
#endregion 父节点
#region 可以构建树形结构
/// <summary>
/// 叶子节点
/// </summary>
protected List<BaseTreeModel> m_listSon = null;
/// <summary>
/// 叶子节点
/// </summary>
/// <returns></returns>
public List<BaseTreeModel> GetListSon()
{
return m_listSon;
}
/// <summary>
/// 叶子节点
/// </summary>
/// <param name="theValue"></param>
public void SetListSon(List<BaseTreeModel> theValue)
{
m_listSon = theValue;
}
#endregion 可以构建树形结构
#region 构造函数
/// <summary>
/// 数据表属性
/// </summary>
/// <param name="p_Parent">父节点|null代表无父节点,自身为根节点</param>
public BaseTreeModel(BaseTreeModel p_Parent)
{
vm_Parent = p_Parent;
}
/// <summary>
/// 数据表属性
/// </summary>
/// <param name="p_Parent">父节点|null代表无父节点,自身为根节点</param>
/// <param name="dr">一条记录</param>
public BaseTreeModel(BaseTreeModel p_Parent, DataRow dr)
{
vm_Parent = p_Parent;
InitBaseTreeModel("", "", dr, false);
}
/// <summary>
/// 数据表属性
/// </summary>
/// <param name="p_Parent">父节点|null代表无父节点,自身为根节点</param>
/// <param name="dr">一条记录</param>
/// <param name="isThrowException">是否抛出异常</param>
public BaseTreeModel(BaseTreeModel p_Parent, DataRow dr, bool isThrowException)
{
vm_Parent = p_Parent;
InitBaseTreeModel("", "", dr, isThrowException);
}
/// <summary>
/// 数据表属性
/// </summary>
/// <param name="p_Parent">父节点|null代表无父节点,自身为根节点</param>
/// <param name="preName">字段前缀缀填充符|如:T__</param>
/// <param name="postName">字段后缀填充符|如:__T</param>
/// <param name="dr">一条记录</param>
public BaseTreeModel(BaseTreeModel p_Parent, string preName, string postName, DataRow dr)
{
vm_Parent = p_Parent;
InitBaseTreeModel(preName, postName, dr, false);
}
/// <summary>
/// 数据表属性
/// </summary>
/// <param name="p_Parent">父节点|null代表无父节点,自身为根节点</param>
/// <param name="preName">字段前缀缀填充符|如:T__</param>
/// <param name="postName">字段后缀填充符|如:__T</param>
/// <param name="dr">一条记录</param>
/// <param name="isThrowException">是否抛出异常</param>
public BaseTreeModel(BaseTreeModel p_Parent, string preName, string postName, DataRow dr, bool isThrowException)
{
vm_Parent = p_Parent;
InitBaseTreeModel(preName, postName, dr, isThrowException);
}
#endregion 构造函数
#region 初始化BaseTreeModel(创建值)
/// <summary>
/// 通过DataRow设置model的属性值
/// </summary>
/// <param name="preName">字段前缀缀填充符|如:T__</param>
/// <param name="postName">字段后缀填充符|如:__T</param>
/// <param name="dr">一条记录</param>
/// <param name="isThrowException">是否抛出异常</param>
public virtual void InitBaseTreeModel
(
string preName,
string postName,
DataRow dr,
bool isThrowException
)
{
DataTable dt = dr.Table;
bool isPreName = false;
bool isPostName = false;
int preNameLen = 0;
int postNameLen = 0;
if (preName == null)
{
preName = "";
}
else
{
preNameLen = preName.Length;
isPreName = (preNameLen > 0);
}
if (postName == null)
{
postName = "";
}
else
{
postNameLen = postName.Length;
isPostName = (postNameLen > 0);
}
if (!isPreName && !isPostName)
{
foreach (DataColumn dc in dt.Columns)
{
SetPropertyValue
(
dc.ColumnName,
isThrowException,
dr[dc.ColumnName]
);
}
}
else
{
string strName = null;
foreach (DataColumn dc in dt.Columns)
{
strName = dc.ColumnName;
if (isPreName)
{
if (!strName.StartsWith(preName))
{
continue;
}
strName = strName.Substring(preNameLen);
}
if (isPostName)
{
if (!strName.EndsWith(postName))
{
continue;
}
strName = strName.Substring(0, strName.Length - postNameLen);
}
SetPropertyValue
(
strName,
isThrowException,
dr[dc.ColumnName]
);
}
}
}
#endregion 初始化BaseTreeModel(创建值)
#region 获得值和取值
/// <summary>
/// 获得属性值 - 如果找不到对应的_propertyName,抛出异常
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public virtual object GetPropertyValue
(
string _propertyName
)
{
Type thisType = this.GetType();
PropertyInfo info = thisType.GetProperty
(
_propertyName,
BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance
);
if (info == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "PropertyInfo info = thisType.GetProperty(_propertyName[" + _propertyName + "]);"
);
}
return info.GetValue(this, null);
}
/// <summary>
/// 获得属性值 - 如果找不到对应的_propertyName,throwException属性判断是否抛出异常
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <param name="throwException">找不到记录是否抛出异常</param>
/// <param name="defaultValue">如果不抛出异常,默认返回值</param>
/// <returns></returns>
public virtual object GetPropertyValue
(
string _propertyName,
bool throwException,
object defaultValue
)
{
Type thisType = this.GetType();
PropertyInfo info = thisType.GetProperty
(
_propertyName,
BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance
);
if (info == null)
{
if (throwException)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "PropertyInfo info = thisType.GetProperty(_propertyName[" + _propertyName + "]);"
);
}
return defaultValue;
}
return info.GetValue(this, null);
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public string GetPropertyStrValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
return null;
return oValue.ToString();
}
/// <summary>
/// 获得属性在Str值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <param name="throwException"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public string GetPropertyStrValue
(
string _propertyName,
bool throwException,
string defaultValue
)
{
object oValue = GetPropertyValue
(
_propertyName,
throwException,
defaultValue
);
if (oValue == null)
return null;
return oValue.ToString();
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public int GetPropertyIntValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为null"
);
}
string str = oValue.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为空"
);
}
int theResult = 0;
if (!int.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效数字"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public int? GetPropertyIntNullValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
return null;
}
string str = oValue.ToString();
if (str.Length == 0)
{
return null;
}
int theResult = 0;
if (!int.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效数字"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public long GetPropertyLongValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为null"
);
}
string str = oValue.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为空"
);
}
long theResult = 0;
if (!long.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效数字"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public long? GetPropertyLongNullValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
return null;
}
string str = oValue.ToString();
if (str.Length == 0)
{
return null;
}
long theResult = 0;
if (!long.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效数字"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public double GetPropertyDoubleValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为null"
);
}
string str = oValue.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为空"
);
}
double theResult = 0;
if (!double.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效数字"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public double? GetPropertyDoubleNullValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
return null;
}
string str = oValue.ToString();
if (str.Length == 0)
{
return null;
}
double theResult = 0;
if (!double.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效数字"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public DateTime GetPropertyDateTimeValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为null"
);
}
string str = oValue.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为空"
);
}
DateTime theResult = DateTime.MinValue;
if (!DateTime.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效时间"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public DateTime? GetPropertyDateTimeNullValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
return null;
}
string str = oValue.ToString();
if (str.Length == 0)
{
return null;
}
DateTime theResult = DateTime.MinValue;
if (!DateTime.TryParse(str, out theResult))
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) == " + str + " 为无效时间"
);
}
return theResult;
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public bool GetPropertyBooleanValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "传入参数:object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为null"
);
}
string str = oValue.ToString();
if (str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为空"
);
}
if (oValue is Boolean)
return (bool)oValue;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为无效布尔值"
);
}
/// <summary>
/// 获得属性值
/// </summary>
/// <param name="_propertyName">字段名(如:TheName)/大小写模糊</param>
/// <returns></returns>
public bool? GetPropertyBooleanNullValue(string _propertyName)
{
object oValue = GetPropertyValue(_propertyName);
if (oValue == null)
{
return null;
}
if (oValue is Boolean)
return (bool)oValue;
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "object oValue = GetPropertyValue(_propertyName[" + _propertyName + "]) 为无效布尔值"
);
}
/// <summary>
/// 获得ID属性值
/// </summary>
/// <returns></returns>
public string GetPropertyStrID()
{
object oValue = GetPropertyValue("id");
if (oValue == null)
return null;
return oValue.ToString();
}
/// <summary>
/// 获得ID属性值
/// </summary>
/// <returns></returns>
public virtual string GetPK()
{
object oValue = GetPropertyValue("id");
if (oValue == null)
return null;
return oValue.ToString();
}
/// <summary>
/// 获得thename(主显字段)属性值
/// </summary>
/// <returns></returns>
public virtual string GetShowName()
{
object oValue = GetPropertyValue("thename");
if (oValue == null)
return null;
return oValue.ToString();
}
#endregion 获得属性值
#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
/// <summary>
/// 获得ID属性值
/// </summary>
/// <returns></returns>
public long GetPropertyLongID()
{
object oValue = GetPropertyValue("id");
if (oValue == null)
throw new Exception("方法:" + MethodBase.GetCurrentMethod().ReflectedType.FullName + " " + MethodBase.GetCurrentMethod().ToString() + " 发生异常:" + "object oValue = GetPropertyValue(\"id\") 为空。");
return long.Parse(oValue.ToString());
}
/// <summary>
/// 获得TheName属性值
/// </summary>
/// <returns></returns>
public string GetPropertyStrTheName()
{
object oValue = GetPropertyValue("thename");
if (oValue == null)
return null;
return oValue.ToString();
}
/// <summary>
/// 获得关键字的值,字段名默认为id
/// </summary>
/// <returns></returns>
public virtual string GetPkValue()
{
object oValue = GetPropertyValue("id");
if (oValue == null)
return null;
return oValue.ToString();
}
}
}
上一条:
下一条:
相关评论
发表评论