TableInfo<T> or TypeInfo


    class TableInfo<T> where T : new()
{
public TableInfo()
{
Type type = typeof(T);
TableAttribute tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType() == typeof(TableAttribute)).SingleOrDefault() as TableAttribute;
if (tableattr != null)
TableName = tableattr.Name;
else
TableName = type.Name;

PropertyInfo[] infos = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (PropertyInfo info in infos)
{
object[] attributes = info.GetCustomAttributes(false);
if (!attributes.Where(attr => attr.GetType() == typeof(IgnoreAttribute)).Any())
{
KeyAttribute key = attributes.Where(attr => attr.GetType() == typeof(KeyAttribute)).SingleOrDefault() as KeyAttribute;
bool isKey = key != null;
if (isKey && key.IsIdentity) IdentityProperty = info.Name;
ColumnAttribute columnMap = attributes.Where(attr => attr.GetType() == typeof(ColumnAttribute)).SingleOrDefault() as ColumnAttribute;
string columnName = info.Name;
string queryName = columnName;
if (columnMap != null)
{
columnName = columnMap.Name;
queryName = columnMap.QueryName;
}
columns.Add(new ColumnMap(columnName, info.Name, queryName, isKey, !string.IsNullOrEmpty(IdentityProperty)));

delegateSet[info.Name] = info.SetValue;
delegateGet[info.Name] = info.GetValue;
selectText = "select " + GetColumnList() + " from " + TableName;

var s = InsertParams();
insertText = "insert into " + TableName + "(" + s[0] + ") values(" + s[1] + ")";
s = SetParams();
updateText = "update " + TableName + " set " + s[0] + " where " + s[1];
}
}
}

public string TableName { get; set; }
string IdentityProperty = "";
string selectText;
string insertText;
string updateText;
List<ColumnMap> columns = new List<ColumnMap>();
Dictionary<string, Action<object, object, object[]>> delegateSet = new Dictionary<string, Action<object, object, object[]>>();
Dictionary<string, Func<object, object[], object>> delegateGet = new Dictionary<string, Func<object, object[], object>>();
Dictionary<string, int> readerIndex = new Dictionary<string, int>();

class ColumnMap
{
public ColumnMap()
{
}

public ColumnMap(string propertyName)
{
ColumnName = propertyName;
PropertyName = propertyName;
QueryName = propertyName;
IsPerimaryKey = false;
}

public ColumnMap(string propertyName, bool isPerimaryKey)
: this(propertyName)
{
IsPerimaryKey = isPerimaryKey;
}

//public ColumnMap(string propertyName, bool isPerimaryKey,bool isIdentity)
// : this(propertyName, isPerimaryKey)
//{
// IsIdentity = isIdentity;
//}

public ColumnMap(string columnName, string propertyName, string queryName, bool isPerimaryKey,bool isIdentity) : this(columnName,isPerimaryKey)
{
ColumnName = columnName;
QueryName = queryName;
}

public string ColumnName { get; set; }
public string PropertyName { get; set; }
public string QueryName { get; set; }
public bool IsPerimaryKey { get; set; }
public bool IsIdentity { get; set; }
}

public string SelectText
{
get
{
return selectText;
}
}

string GetColumnList()
{
List<string> columnList = new List<string>();
foreach (ColumnMap map in columns)
{
if (map.ColumnName == map.QueryName)
columnList.Add(map.ColumnName);
else
columnList.Add(map.ColumnName + " as " + map.QueryName);
}
return SSGClass.DataConvert.JoinList(columnList);
}

public string DeleteText
{
get
{
return "delete from " + TableName;
}
}

public SSGClass.ExpressOpr GetDeleteExpress(T entity)
{
SSGClass.ExpressOpr exp = null;
var keys = columns.Where(col => col.IsPerimaryKey == true);
if (keys.Any())
{
foreach (var keyColumn in keys)
{
if (exp == null)
exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
else
exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
}
}
else
{
foreach (var keyColumn in columns)
{
if (exp == null)
exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
else
exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
}
}
return exp;
}

public string InsertText
{
get
{
return insertText;
}
}

public Dictionary<string, object> GetEntityPatams(T entity)
{
Dictionary<string, object> paramValues = new Dictionary<string,object>();
foreach (ColumnMap map in columns)
{
if (!string.IsNullOrEmpty(map.ColumnName))
paramValues.Add("@" + map.ColumnName, delegateGet[map.PropertyName].Invoke(entity, null));
}
return paramValues;
}

string[] InsertParams()
{
string[] param = new string[2];
List<string> columnList = new List<string>();
List<string> paramList = new List<string>();
foreach (ColumnMap map in columns)
{
if (!string.IsNullOrEmpty(map.ColumnName))
{
columnList.Add(map.ColumnName);
paramList.Add("@" + map.ColumnName);
}
}
param[0] = SSGClass.DataConvert.JoinList(columnList);
param[1] = SSGClass.DataConvert.JoinList(paramList);
return param;
}

public string UpdateText
{
get
{
return updateText;
}
}

string[] SetParams()
{
string[] param = new string[2];
List<string> columnList = new List<string>();
List<string> paramList = new List<string>();
foreach (ColumnMap map in columns)
{
if (map.IsPerimaryKey)
paramList.Add(map.ColumnName + " = @" + map.ColumnName);
else
columnList.Add(map.ColumnName + " = @" + map.ColumnName);
}
param[0] = SSGClass.DataConvert.JoinList(columnList);
param[1] = SSGClass.DataConvert.JoinList(paramList);
return param;
}

public T GenEntity(System.Data.IDataReader reader)
{
T entity = new T();
foreach (ColumnMap map in columns)
{
int index;
if (!readerIndex.TryGetValue(map.QueryName, out index))
{
index = reader.GetOrdinal(map.QueryName);
readerIndex[map.QueryName] = index;
}
object value = reader[index];
if(value == DBNull.Value)
value = SSGClass.DataConvert.GetValue(value,value.GetType());
delegateSet[map.PropertyName].Invoke(entity, value, null);
}
return entity;
}

public T GenEntity(System.Data.DataRow row)
{
T entity = new T();
foreach (ColumnMap map in columns)
{
object value = row[map.QueryName];
if (value == DBNull.Value)
value = SSGClass.DataConvert.GetValue(value, value.GetType());
delegateSet[map.PropertyName].Invoke(entity, value, null);
}
return entity;
}
}

第一版本的泛型发射信息设计,后来发觉每次建立对象的时候都需要T信息,而object中必然存在Type信息,而通过Type来读写信息本来就是核心的功能,因此修改为TypeInfo


    class TypeInfo
{
public TypeInfo()
{

}
public TypeInfo(Type type)
{
TableAttribute tableattr = type.GetCustomAttributes(false).Where(attr => attr.GetType() == typeof(TableAttribute)).SingleOrDefault() as TableAttribute;
if (tableattr != null)
TableName = tableattr.Name;
else
TableName = type.Name;

PropertyInfo[] infos = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
foreach (PropertyInfo info in infos)
{
object[] attributes = info.GetCustomAttributes(false);
if (!attributes.Where(attr => attr.GetType() == typeof(IgnoreAttribute)).Any())
{
KeyAttribute key = attributes.Where(attr => attr.GetType() == typeof(KeyAttribute)).SingleOrDefault() as KeyAttribute;
bool isKey = key != null;
if (isKey && key.IsIdentity) IdentityProperty = info.Name;
ColumnAttribute columnMap = attributes.Where(attr => attr.GetType() == typeof(ColumnAttribute)).SingleOrDefault() as ColumnAttribute;
string columnName = info.Name;
string queryName = columnName;
if (columnMap != null)
{
columnName = columnMap.Name;
queryName = columnMap.QueryName;
}
columns.Add(new ColumnMap(columnName, info.Name, queryName, isKey, !string.IsNullOrEmpty(IdentityProperty)));

delegateSet[info.Name] = info.SetValue;
delegateGet[info.Name] = info.GetValue;
selectText = "select " + GetColumnList() + " from " + TableName;

var s = InsertParams();
insertText = "insert into " + TableName + "(" + s[0] + ") values(" + s[1] + ")";
s = SetParams();
updateText = "update " + TableName + " set " + s[0] + " where " + s[1];
}
}
}

public string TableName { get; set; }
string IdentityProperty = "";
string selectText;
string insertText;
string updateText;
List<ColumnMap> columns = new List<ColumnMap>();
Dictionary<string, Action<object, object, object[]>> delegateSet = new Dictionary<string, Action<object, object, object[]>>();
Dictionary<string, Func<object, object[], object>> delegateGet = new Dictionary<string, Func<object, object[], object>>();
Dictionary<string, int> readerIndex = new Dictionary<string, int>();

class ColumnMap
{
public ColumnMap()
{
}

public ColumnMap(string propertyName)
{
ColumnName = propertyName;
PropertyName = propertyName;
QueryName = propertyName;
IsPerimaryKey = false;
}

public ColumnMap(string propertyName, bool isPerimaryKey)
: this(propertyName)
{
IsPerimaryKey = isPerimaryKey;
}

public ColumnMap(string columnName, string propertyName, string queryName, bool isPerimaryKey, bool isIdentity)
: this(columnName, isPerimaryKey)
{
ColumnName = columnName;
QueryName = queryName;
}

public string ColumnName { get; set; }
public string PropertyName { get; set; }
public string QueryName { get; set; }
public bool IsPerimaryKey { get; set; }
public bool IsIdentity { get; set; }
}

public string SelectText
{
get
{
return selectText;
}
}

string GetColumnList()
{
List<string> columnList = new List<string>();
foreach (ColumnMap map in columns)
{
if (map.ColumnName == map.QueryName)
columnList.Add(map.ColumnName);
else
columnList.Add(map.ColumnName + " as " + map.QueryName);
}
return SSGClass.DataConvert.JoinList(columnList);
}

public string DeleteText
{
get
{
return "delete from " + TableName;
}
}

public SSGClass.ExpressOpr GetDeleteExpress(object entity)
{
SSGClass.ExpressOpr exp = null;
var keys = columns.Where(col => col.IsPerimaryKey == true);
if (keys.Any())
{
foreach (var keyColumn in keys)
{
if (exp == null)
exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
else
exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
}
}
else
{
foreach (var keyColumn in columns)
{
if (exp == null)
exp = SSGClass.ExpressOpr.where(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
else
exp += SSGClass.ExpressOpr.and(keyColumn.ColumnName, Op.Equal, delegateGet[keyColumn.PropertyName].Invoke(entity, null));
}
}
return exp;
}

public string InsertText
{
get
{
return insertText;
}
}

public Dictionary<string, object> GetEntityPatams(object entity)
{
Dictionary<string, object> paramValues = new Dictionary<string, object>();
foreach (ColumnMap map in columns)
{
if (!string.IsNullOrEmpty(map.ColumnName))
paramValues.Add("@" + map.ColumnName, delegateGet[map.PropertyName].Invoke(entity, null));
}
return paramValues;
}

string[] InsertParams()
{
string[] param = new string[2];
List<string> columnList = new List<string>();
List<string> paramList = new List<string>();
foreach (ColumnMap map in columns)
{
if (!string.IsNullOrEmpty(map.ColumnName))
{
columnList.Add(map.ColumnName);
paramList.Add("@" + map.ColumnName);
}
}
param[0] = SSGClass.DataConvert.JoinList(columnList);
param[1] = SSGClass.DataConvert.JoinList(paramList);
return param;
}

public string UpdateText
{
get
{
return updateText;
}
}

string[] SetParams()
{
string[] param = new string[2];
List<string> columnList = new List<string>();
List<string> paramList = new List<string>();
foreach (ColumnMap map in columns)
{
if (map.IsPerimaryKey)
paramList.Add(map.ColumnName + " = @" + map.ColumnName);
else
columnList.Add(map.ColumnName + " = @" + map.ColumnName);
}
param[0] = SSGClass.DataConvert.JoinList(columnList);
param[1] = SSGClass.DataConvert.JoinList(paramList);
return param;
}

public T GenEntity<T>(System.Data.IDataReader reader) where T : new()
{
T entity = new T();
foreach (ColumnMap map in columns)
{
int index;
if (!readerIndex.TryGetValue(map.QueryName, out index))
{
index = reader.GetOrdinal(map.QueryName);
readerIndex[map.QueryName] = index;
}
object value = reader[index];
if (value == DBNull.Value)
value = SSGClass.DataConvert.GetValue(value, value.GetType());
delegateSet[map.PropertyName].Invoke(entity, value, null);
}
return entity;
}

public T GenEntity<T>(System.Data.DataRow row) where T : new()
{
T entity = new T();
foreach (ColumnMap map in columns)
{
object value = row[map.QueryName];
if (value == DBNull.Value)
value = SSGClass.DataConvert.GetValue(value, value.GetType());
delegateSet[map.PropertyName].Invoke(entity, value, null);
}
return entity;
}

}

TableInfo<T>
or TypeInfo

时间: 2024-07-28 15:36:01

TableInfo<T> or TypeInfo的相关文章

枚举类型互相转换(使用GetEnumName和TypeInfo两个函数)

usesClasses,TypInfo ; typeTCommandType = (ctEmptyCommand,ctAdd,ctModify); TCommandTypeConvert=classpublic    class function CommandToString(ACommand: TCommandType): string;    class function StringToCommand(const AStrCommand: string): TCommandType;en

_Dispose(typeinfo,pointer ); 不知道说的是什么? 感觉会有用, 留待以后研究

[传说]晓不得2013(26562729)  16:45:41别人把文章发出来,说明就是验证过的.[潜水]ひㄨㄨ那个ㄨㄨ(1548253108)  16:46:23 [潜水]ひㄨㄨ那个ㄨㄨ(1548253108)  16:46:27这个是代码[传说]晓不得2013(26562729)  16:46:31不看源码,不远程.这是原则.这里是程序交流区,不是源代码交流区.更多的是灵魂交流,你懂的.[传说]晓不得2013(26562729)  16:47:40,一个功能实现了,我只说一句这个.就这一句,

undefined reference to typeinfo - C++ error message

There are some compiler and loader error messages that shout obviously as to their cause, but there are others that simply don't give the new user much of an indication as to what's really wrong. And most of those I get to know pretty quickly, so tha

error:对‘vtable for new_sequence’未定义的引用 对‘typeinfo for num_sequence’未定义的引用

在设计父类子类继承关系中,经常会出现此类问题. 报错原因:父类中的虚函数只有声明,没有定义. 解决方案 : 1. 定义相关的虚函数的实现. 2. 不实现了,直接搞成纯虚函数留给后代实现. virtual int getlength()const = 0; p,li { white-space: pre-wrap }

PostgreSQL逻辑备份pg_dump使用及其原理解析

一.原理分析 1.循环调用getopt_long解析命令行参数,将参数保存到static DumpOptions dopt;中2.判断参数是否相容,不相容则退出: options -s/--schema-only and -a/--data-only cannot be used together options -c/--clean and -a/--data-only cannot be used together options --inserts/--column-inserts and

c++中RTTI

RTTI 是"Runtime Type Information"的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通过具体例子和代码介绍什么时候使用以及如何使用 RTTI:本文还将详细描述两个重要的 RTTI 运算符的使用方法,它们是 typeid 和 dynamic_cast.如何确定对象的动态类型呢?答案是使用内建的 RTTI 中的运算符:typeid 和 dynamic_cast. typeid的

ClientDataSet中动态添加计算字段并用计算字段显示记录的UpdateStatus

ClientDataSet中每条记录都有UpdateStatus=(usUnmodified, usModified, usInserted, usDeleted)记录该条数据是修改的,删除的,还是新增的等.有时候我们只想看修改的或新增的或删除的就可能用到这一属性.下图用计算字段显示UpdateStatus状态. ********************************************************************************************

SOAP 格式设置选项

SOAP 格式设置选项 两个格式设置选项为: Style:适用于 SOAP 消息中 Body 元素的子元素(也可能是孙级).此选项指定为 binding WSDL 元素(通常情况下)或 operation 元素的 style 属性. Use:适用于出现在下一个级别的 Web 服务方法参数(或返回值).此选项指定为 body 元素的 use 属性. 有关 SOAP 规范的详细信息,请访问 W3C 网站 (http://www.w3.org/TR/SOAP).有关 WSDL 规范的详细信息,也可以访

bboss自动代码生成工具使用指南

本文介绍bboss自动代码生成工具使用方法 工具在线浏览效果: http://gencode.bbossgroups.com 在介绍之前首先了解一下bboss自动代码生成工具能帮助我们做哪些事情. 通过自动代码生成框架,根据模板可以自动生成数据库表的增.删.改.分页查询.列表查询.国际化功能对应的java.jsp程序和配置文件,包括: 1.mvc控制器 2.业务组件 3.PO实体类 4.jsp文件 可以定制不同风格的界面模板,目前提供了一套bboss平台的基础ui风格和一套bboss普通ui风格