DomainObjectUtility
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.Text; namespace LZHBaseFrame.Core.Domain
{ public class DomainObjectUtility
{
public static void CheckDomainObject(object domainObject, IDomainDataProvider domainObjectDataProvider)
{
if (!(domainObject is DomainObject))
{
throw new Exception(string.Format("$Error_Should_Be_DomainObject[$DomainObject={0}]", domainObject));
}
}
public static object CreateTypeInstance(Type type)
{
return type.Assembly.CreateInstance(type.FullName);
}
public static ArrayList GetMemberInfos(Type type)
{
ArrayList list1 = new ArrayList();
MemberInfo[] infoArray1 = type.GetMembers();
for (int num1 = ; num1 < infoArray1.Length; num1++)
{
MemberInfo info1 = infoArray1[num1]; if (((info1.MemberType == MemberTypes.Field) || (info1.MemberType == MemberTypes.Property)) && (info1.IsDefined(typeof(FieldMapAttribute), true) || info1.IsDefined(typeof(FieldMapAttribute), false)))
{
list1.Add(info1);
}
}
return list1;
} public static ArrayList GetMemberInfos(object obj)
{
return DomainObjectUtility.GetMemberInfos(obj.GetType());
} public static Hashtable GetKeyAttributeMemberInfos(object obj)
{
return DomainObjectUtility.GetKeyAttributeMemberInfos(DomainObjectUtility.GetTableMapAttribute(obj), DomainObjectUtility.GetMemberInfos(obj));
} public static Hashtable GetKeyAttributeMemberInfos(Type type)
{
return DomainObjectUtility.GetKeyAttributeMemberInfos(DomainObjectUtility.GetTableMapAttribute(type), DomainObjectUtility.GetMemberInfos(type));
} public static Hashtable GetKeyAttributeMemberInfos(TableMapAttribute tableMapAttribute, ArrayList memberInfos)
{
StringCollection keyFields = new StringCollection();
keyFields.AddRange(tableMapAttribute.GetKeyFields());
Hashtable hs = new Hashtable(); foreach (MemberInfo info1 in memberInfos)
{
FieldMapAttribute attribute = (FieldMapAttribute)System.Attribute.GetCustomAttribute(info1, typeof(FieldMapAttribute));
if (keyFields.IndexOf(attribute.FieldName) != -)
{
hs.Add(attribute, info1);
}
}
return hs;
} public static Hashtable GetNonKeyAttributeMemberInfos(object obj)
{
return DomainObjectUtility.GetNonKeyAttributeMemberInfos(DomainObjectUtility.GetTableMapAttribute(obj), DomainObjectUtility.GetMemberInfos(obj));
} public static Hashtable GetNonKeyAttributeMemberInfos(Type type)
{
return DomainObjectUtility.GetNonKeyAttributeMemberInfos(DomainObjectUtility.GetTableMapAttribute(type), DomainObjectUtility.GetMemberInfos(type));
} public static Hashtable GetNonKeyAttributeMemberInfos(TableMapAttribute tableMapAttribute, ArrayList memberInfos)
{
StringCollection keyFields = new StringCollection();
keyFields.AddRange(tableMapAttribute.GetKeyFields());
Hashtable hs = new Hashtable(); foreach (MemberInfo info1 in memberInfos)
{
FieldMapAttribute attribute = (FieldMapAttribute)System.Attribute.GetCustomAttribute(info1, typeof(FieldMapAttribute));
if (keyFields.IndexOf(attribute.FieldName) == -)
{
hs.Add(attribute, info1);
}
}
return hs;
} private static Hashtable hashtableAttributeMember = new Hashtable();
public static Hashtable GetAttributeMemberInfos(object obj)
{
lock (hashtableAttributeMember)
{
if (hashtableAttributeMember.ContainsKey(obj.GetType().FullName) == false)
{
hashtableAttributeMember.Add(obj.GetType().FullName, DomainObjectUtility.GetAttributeMemberInfos(DomainObjectUtility.GetMemberInfos(obj)));
}
}
return (Hashtable)hashtableAttributeMember[obj.GetType().FullName];
} public static Hashtable GetAttributeMemberInfos(Type type)
{
lock (hashtableAttributeMember)
{
if (hashtableAttributeMember.ContainsKey(type.FullName) == false)
{
hashtableAttributeMember.Add(type.FullName, DomainObjectUtility.GetAttributeMemberInfos(DomainObjectUtility.GetMemberInfos(type)));
}
}
return (Hashtable)hashtableAttributeMember[type.FullName];
} private static Hashtable GetAttributeMemberInfos(ArrayList memberInfos)
{
Hashtable hs = new Hashtable(); foreach (MemberInfo info1 in memberInfos)
{
FieldMapAttribute attribute = (FieldMapAttribute)System.Attribute.GetCustomAttribute(info1, typeof(FieldMapAttribute));
try
{
hs.Add(attribute, info1);
}
catch (Exception)
{ throw new Exception("$Error_To_add_columns_is_repeat");
} }
return hs;
} public static TableMapAttribute GetTableMapAttribute(Type type)
{
object attribute = System.Attribute.GetCustomAttribute(type, typeof(TableMapAttribute));
return ((attribute != null) ? (TableMapAttribute)attribute : null);
} public static TableMapAttribute GetTableMapAttribute(object obj)
{
return DomainObjectUtility.GetTableMapAttribute(obj.GetType());
} public static DomainObject[] FillDomainObject(Type type, DataSet ds)
{
if (ds == null)
{
return null;
} if (ds.Tables[].Rows.Count < )
{
return null;
} return DomainObjectUtility.FillDomainObject(type, ds.Tables[]);
} private static DomainObject[] FillDomainObject(Type type, DataTable table)
{
DomainObject[] domainObjects = new DomainObject[table.Rows.Count];
int num1 = ;
foreach (DataRow dr in table.Rows)
{
domainObjects[num1] = DomainObjectUtility.FillDomainObject(type.Assembly.CreateInstance(type.FullName), dr);
num1 = num1 + ;
} return domainObjects;
} public static DomainObject FillDomainObject(object obj, DataRow dataRow)
{
Hashtable hs = DomainObjectUtility.GetAttributeMemberInfos(obj);
Hashtable hsFields = GetFieldNameMapAttribute(obj.GetType().FullName, hs); foreach (DataColumn column in dataRow.Table.Columns)
{
if (hsFields.Contains(column.ColumnName.ToUpper()))
{
DomainObjectUtility.SetValue(obj, (MemberInfo)hsFields[column.ColumnName.ToUpper()], dataRow[column.ColumnName]);
}
} return (DomainObject)obj;
}
private static Hashtable hashtableFieldNameMap = new Hashtable();
private static Hashtable GetFieldNameMapAttribute(string typeFullName, Hashtable hs)
{
lock (hashtableFieldNameMap)
{
if (hashtableFieldNameMap.ContainsKey(typeFullName) == false)
{
Hashtable hsFields = new Hashtable(hs.Count);
foreach (FieldMapAttribute fa in hs.Keys)
{
hsFields.Add(fa.FieldName.ToUpper(), hs[fa]);
}
hashtableFieldNameMap.Add(typeFullName, hsFields);
}
}
return (Hashtable)hashtableFieldNameMap[typeFullName];
} public static Hashtable GetFieldNameMemberInfoMap(Type type)
{
Hashtable returnValue = new Hashtable(); foreach (MemberInfo info in type.GetMembers())
{
FieldMapAttribute attribute = (FieldMapAttribute)System.Attribute.GetCustomAttribute(info, typeof(FieldMapAttribute)); if (attribute != null)
{
returnValue.Add(attribute.FieldName.Trim().ToLower(), info);
}
} return returnValue;
} public static object CSharpValue2DbValue(Type dataType, Type propertyType, object value)
{
if (propertyType == typeof(int))
{
int num1 = Convert.ToInt32(value);
return num1;
} if (propertyType == typeof(decimal))
{
Decimal num1 = Convert.ToDecimal(value);
return num1;
} if (propertyType == typeof(long))
{
long num1 = (long)value;
return num1.ToString();
} if (propertyType == typeof(string))
{
if (value == null)
{
return "";
}
return value.ToString();
} if (propertyType == typeof(bool))
{
if (!((bool)value))
{
return "N";
}
return "Y";
} if (propertyType == typeof(DateTime))
{
DateTime time1 = (DateTime)value;
return time1.ToString("yyyy/MM/dd HH:mm:ss");
} if (propertyType == typeof(byte[]))
{
return value;
} return value.ToString(); } public static string XMLEncodeValue(Type propertyType, object value)
{
if (propertyType == typeof(int))
{
int num1 = (int)value;
return num1.ToString();
}
if (propertyType == typeof(string))
{
if (value == null)
{
return "";
}
return value.ToString();
}
if (propertyType == typeof(bool))
{
if (!((bool)value))
{
return "N";
}
return "Y";
} if (propertyType == typeof(DateTime))
{
DateTime time1 = (DateTime)value;
return (time1.ToString("yyyy/MM/dd HH:mm:ss"));
}
return value.ToString();
} public static object GetValue(object obj, MemberInfo mi, object[] index)
{
if (mi is FieldInfo)
{
return ((FieldInfo)mi).GetValue(obj);
}
if (mi is PropertyInfo)
{
return ((PropertyInfo)mi).GetValue(obj, index);
} return null;
} public static object GetValue(object domainObject, string propertyName, object[] index)
{
MemberInfo[] infos = domainObject.GetType().GetMember(propertyName); if (infos == null)
{
throw new Exception("$Error_Property_Name_Not_Exist");
return null;
} return GetValue(domainObject, infos[], index);
} public static void SetValue(object domainObject, string propertyName, object value)
{
ArrayList infos = DomainObjectUtility.GetMemberInfos(domainObject);
MemberInfo info1 = null; foreach (MemberInfo info in infos)
{
if (info.Name == propertyName)
{
info1 = info;
break;
}
} if (info1 == null)
{
throw new Exception("$Error_Property_Name_Not_Exist");
} DomainObjectUtility.SetValue(domainObject, info1, value);
} private static void SetValue(object domainObject, MemberInfo info, object value)
{
if (info == null)
{
return;
} FieldMapAttribute fa = (FieldMapAttribute)System.Attribute.GetCustomAttribute(info, typeof(FieldMapAttribute)); if (fa == null)
{
return;
} Type type1 = (info is FieldInfo) ? ((FieldInfo)info).FieldType : ((PropertyInfo)info).PropertyType;
if (fa.BlobType == BlobTypes.Binary)
{
if (!((DomainObject)domainObject).IsBlobIgnored)
{
DomainObjectUtility.SetValue(domainObject, info, value, null);
return;
}
} if (type1 == typeof(int))
{
if (value is System.DBNull)
{
return;
}
else
{
DomainObjectUtility.SetValue(domainObject, info, System.Int32.Parse(value.ToString()), null);
return;
}
} if (type1 == typeof(long))
{
if (value is System.DBNull)
{
return;
} DomainObjectUtility.SetValue(domainObject, info, System.Int64.Parse(value.ToString()), null);
return;
} if (type1 == typeof(double))
{
if (value is System.DBNull)
{
return;
} DomainObjectUtility.SetValue(domainObject, info, System.Double.Parse(value.ToString()), null);
return;
} if (type1 == typeof(float))
{
if (value is System.DBNull)
{
return;
} DomainObjectUtility.SetValue(domainObject, info, System.Single.Parse(value.ToString()), null);
return;
} if (type1 == typeof(decimal))
{
if (value is System.DBNull)
{
return;
} //DomainObjectUtility.SetValue(domainObject, info, System.Decimal.Parse(DomainObjectUtility.GetValueString(value)), null);
//说明: 以前的方法在高精度的double数据转换成decimal数据的时候,报参数类型不对的错误,原因是高精度的double的数据在ToString之后会编程科学记数法,因此decimal.parse的时候会出错
//解决方法: 私有方法专门对
DomainObjectUtility.SetValue(domainObject, info, DomainObjectUtility.Getdecimal(value), null);
return;
} if (type1 == typeof(bool))
{
DomainObjectUtility.SetValue(domainObject, info, value.ToString() == "Y", null);
return;
}
if (type1 == typeof(string))
{
DomainObjectUtility.SetValue(domainObject, info, value.ToString(), null);
return;
} if (type1 == typeof(DateTime))
{
DateTime result;
DateTime.TryParse(value.ToString(), out result);
DomainObjectUtility.SetValue(domainObject, info, result, null); return;
} if (type1.IsEnum)
{
DomainObjectUtility.SetValue(domainObject, info, Enum.Parse(type1, value.ToString()), null);
return;
} DomainObjectUtility.SetValue(domainObject, info, value, null);
} private static decimal Getdecimal(object obj)
{
string returnStr = obj.ToString();
if (obj.GetType() == typeof(double))
{
return (decimal)Math.Round((double)obj, );
} return decimal.Parse(returnStr);
} public static void SetValue(object obj, MemberInfo mi, object value, object[] index)
{
if (mi is FieldInfo)
{
if (value is System.DBNull)
{
((FieldInfo)mi).SetValue(obj, null);
}
else
{ ((FieldInfo)mi).SetValue(obj, value);
}
}
else if (mi is PropertyInfo)
{
if (value is System.DBNull)
{
((PropertyInfo)mi).SetValue(obj, null, index);
}
else
{
((PropertyInfo)mi).SetValue(obj, value, index);
}
}
} /// <summary>
/// 获得DomainObject中定义了FieldMapAttribute的属性名或字段名
/// </summary>
/// <param name="type">DomainObject的类型</param>
/// <returns></returns>
public static ArrayList GetDomainObjectScheme(Type type)
{
ArrayList array = DomainObjectUtility.GetMemberInfos(type);
ArrayList scheme = new ArrayList(array.Count); foreach (MemberInfo info in array)
{
scheme.Add(info.Name);
} return scheme;
} /// <summary>
/// 获得DomainObject中定义了FieldMapAttribute,且为主键的属性名或字段名
/// </summary>
/// <param name="type">DomainObject的类型</param>
/// <returns></returns>
public static string[] GetDomainObjectKeyScheme(Type type)
{
Hashtable ht = DomainObjectUtility.GetKeyAttributeMemberInfos(type);
ArrayList scheme = new ArrayList(ht.Count); foreach (MemberInfo info in ht.Values)
{
scheme.Add(info.Name);
} return (string[])scheme.ToArray(typeof(string));
} /// <summary>
/// 获得DomainObject中定义了FieldMapAttribute的属性名或字段名的值
/// </summary>
/// <param name="obj">DomainObject</param>
/// <returns></returns>
public static ArrayList GetDomainObjectValues(DomainObject obj)
{
ArrayList array = DomainObjectUtility.GetMemberInfos(obj);
ArrayList values = new ArrayList(array.Count); foreach (MemberInfo info in array)
{
values.Add(DomainObjectUtility.GetValue(obj, info, null));
} return values;
} /// <summary>
/// 获得DomainObject中定义了FieldMapAttribute,且为主键的属性名或字段名的值
/// </summary>
/// <param name="obj">DomainObject</param>
/// <returns></returns>
public static object[] GetDomainObjectKeyValues(DomainObject obj)
{
Hashtable ht = DomainObjectUtility.GetKeyAttributeMemberInfos(obj);
ArrayList values = new ArrayList(ht.Count); foreach (MemberInfo info in ht.Values)
{
values.Add(DomainObjectUtility.GetValue(obj, info, null));
} return (object[])values.ToArray(typeof(object));
} /// <summary>
/// 获得DomainObject对应的所有数据库字段名
/// </summary>
/// <param name="type">DomainObject的类型</param>
/// <returns></returns>
public static ArrayList GetDomainObjectFieldsName(Type type)
{
Hashtable hs = DomainObjectUtility.GetAttributeMemberInfos(type);
ArrayList array = new ArrayList(hs.Count); foreach (FieldMapAttribute attr in hs.Keys)
{
array.Add(attr.FieldName);
} return array;
} /// <summary>
/// 获得DomainObject对应的所有数据库字段名,用','拼成的字符串
/// </summary>
/// <param name="type">DomainObject的类型</param>
/// <returns></returns>
public static string GetDomainObjectFieldsString(Type type)
{
return String.Join(", ", (string[])DomainObjectUtility.GetDomainObjectFieldsName(type).ToArray(typeof(string)));
} /// <summary>
/// 获得DomainObject对应的 数据库表名.字段名,用','拼成的字符串
/// </summary>
/// <param name="type">DomainObject的类型</param>
/// <returns></returns>
public static string GetDomainObjectFieldsStringWithTableName(Type type)
{
string tableName = DomainObjectUtility.GetTableMapAttribute(type).TableName;
string[] fieldNames = (string[])DomainObjectUtility.GetDomainObjectFieldsName(type).ToArray(typeof(string)); for (int i = ; i < fieldNames.Length; i++)
{
fieldNames[i] = string.Format("{0}.{1}", tableName, fieldNames[i]);
} return String.Join(", ", fieldNames);
} /// <summary>
/// 将src中各属性的值复制到des中
/// </summary>
/// <param name="src"></param>
/// <param name="des"></param>
public static void MembersClone(DomainObject src, DomainObject des)
{
foreach (MemberInfo info in DomainObjectUtility.GetMemberInfos(src))
{
DomainObjectUtility.SetValue(des, info.Name, DomainObjectUtility.GetValue(src, info, null));
}
} /// <summary>
/// 根据对象的字段名取出数据库中的字段名
/// </summary>
/// <param name="type">domain对象</param>
/// <param name="attributeName">domain对象的字段名</param>
/// <returns>数据库中的字段名</returns>
public static string GetFieldName(Type type, string attributeName)
{
System.Reflection.MemberInfo[] mems = type.GetMembers();
foreach (System.Reflection.MemberInfo mem in mems)
{
if (mem.Name == attributeName)
{
object[] objs = mem.GetCustomAttributes(typeof(LZHBaseFrame.Core.Domain.FieldMapAttribute), true);
if (objs != null && objs.Length > )
{
LZHBaseFrame.Core.Domain.FieldMapAttribute fieldMap = objs[] as LZHBaseFrame.Core.Domain.FieldMapAttribute;
if (fieldMap != null)
{
return fieldMap.FieldName;
}
}
}
}
return null;
} public static string GetPropertyNameByFieldName(Type domainObjectType, string fieldName)
{
string returnValue = string.Empty; if (fieldName.IndexOf(".") >= )
{
fieldName = fieldName.Substring(fieldName.IndexOf(".") + );
} if (fieldName.Trim().Length > )
{
Hashtable fieldNameMemberInfoMap = GetFieldNameMemberInfoMap(domainObjectType);
MemberInfo info = (MemberInfo)fieldNameMemberInfoMap[fieldName.Trim().ToLower()]; if (info != null)
{
returnValue = info.Name;
}
} return returnValue;
}
}
}
DomainObjectUtility的更多相关文章
随机推荐
- lib.tcl
#********************************************************************# 功能描述:定义公共的函数# 依赖关系:依赖于全局aitoo ...
- mysql 分区与性能
数据库应用分为两类: OLTP(在线事务处理):如bolg.电子商务.网络游戏等 OLAP(在线分析处理):如数据仓库.数据集市 对于OLAP,分区可以很好的提高查询性能,应用大多数据需要频繁地扫描一 ...
- Tarjan(lca)
http://codevs.cn/problem/2370 / 2370 小机房的树 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描 ...
- Codeforces 1156E Special Segments of Permutation(单调栈)
可以用单调栈直接维护出ai所能覆盖到的最大的左右范围是什么,然后我们可以用这个范围暴力的去查询这个区间的是否有满足的点对,一个小坑点,要对左右区间的大小进行判断,只需要去枚举距离i最近的一段区间去枚举 ...
- BUUCTF--内涵的软件
测试文件:https://buuoj.cn/files/0450881183f6478e110f9ea27581683b/70125468-0786-4705-bd91-87037f8f3e16.ex ...
- uoj396 [NOI2018]屠龙勇士
[NOI2018]屠龙勇士 描述 小 D 最近在网上发现了一款小游戏.游戏的规则如下: 游戏的目标是按照编号 1∼n 顺序杀掉 n 条巨龙,每条巨龙拥有一个初始的生命值 ai .同时每条巨龙拥有恢复能 ...
- sessionStorage 使用方法
作为html5中Web Storage的一种存储方式,localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. W3c上给的介绍是这两者区别在于前者用于持久化的本地 ...
- 测试微信小程序页面的生命周期
前言:本人是一个初学者,也是第一次写博客,敲键盘的时候还不知道发布后是什么效果,希望内容给其他初学的同学一点帮助,同时加深自己的理解.这篇随笔讲的是Page页面的生命周期,在开发中是基础中的基础,很容 ...
- JavaScript面向对象编程(2)-- 类的定义
最近这一段时间事情太多了,没有时间再继续写,幸好这两天有点小闲,先小写一下JavaScript中面向对象一中推荐的方法.本文承接上一篇JavaScript面向对象编程(1) -- 基础. 上篇说过,J ...
- R语言数据类型与数据结构
一.数据类型 5种 1.character 字符 2.numeric 数值 3.integer 整数 一般数字的存储会默认为数值类型,如果要强调是整数,需要在变量值后面加上 L. x <- 5L ...