在定义泛型类时,可以对客户端代码能够在实例化类时用于类型参数的类型种类施加限制。如果客户端代码尝试使用某个约束所不允许的类型来实例化类,则会产生编译时错误。这些限制称为约束。约束是使用 where 上下文关键字指定的。下表列出了六种类型的约束:
约束 | 说明 |
---|---|
T:结构 |
类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。有关更多信息,请参见使用可空类型(C# 编程指南)。 |
T:类 |
类型参数必须是引用类型,包括任何类、接口、委托或数组类型。 |
T:new() |
类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。 |
T:<基类名> |
类型参数必须是指定的基类或派生自指定的基类。 |
T:<接口名称> |
类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。 |
T:U |
为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束。 |
如果要检查泛型列表中的某个项以确定它是否有效,或者将它与其他某个项进行比较,则编译器必须在一定程度上保证它需要调用的运算符或方法将受到客户端代码可能指定的任何类型参数的支持。这种保证是通过对泛型类定义应用一个或多个约束获得的。例如,基类约束告诉编译器:仅此类型的对象或从此类型派生的对象才可用作类型参数。一旦编译器有了这个保证,它就能够允许在泛型类中调用该类型的方法。约束是使用上下文关键字 where 应用的。
[转]
泛型约束基本上有五种:
- 值类型约束:要求泛型参数必须是值类型,例如int,short以及自定义的stuct等
public class MyClass2
where T : struct//这个泛型类只接受值类型的泛型参数
{
}
- 引用类型约束:要求泛型参数必须是引用类型,例如string,object,以及自定义的class
public class MyClass
where T:class//这个泛型类只接受引用类型的泛型参数
{
}
- 构造函数约束:要求泛型参数必须有构造函数
public class MyClass3
where T : new()
{
}
- 接口约束:要求泛型参数必须实现某个接口
public class MyClass4
where T : System.IComparable
{
}
- 基类约束:要求泛型参数必须继承某个基类
public class MyClass5
where T : Customer
{
}
实战代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Dare.Utilities.Data;
using Dare.DN.Components;
using System.Data;
using log4net.Core;
namespace Dare.DN.Data
{
public abstract class DataProviderTemplate
where Entity : class, IEntity, IEntity
where Key : struct
{
protected List
public DataProviderTemplate()
{
relationDataProviders = new List
}
#region 关系操作方法
public void AddRelationDataProviders(params EntityRelationDataProvider
{
relationDataProviders.AddRange(providers);
}
protected virtual void InsertRelations(TransactionManager manager, DbCommand cmd, IEnumerable
{
if (entities == null) return;
foreach (EntityRelationDataProvider
{
provider.Insert(manager, cmd, entities);
}
}
protected virtual void InsertRelations(TransactionManager manager, DbCommand cmd, params Entity[] entities)
{
InsertRelations(manager, cmd, (IEnumerable
}
protected virtual void UpdateRelations(TransactionManager manager, DbCommand cmd, IEnumerable
{
if (entities == null) return;
foreach (EntityRelationDataProvider
{
provider.Update(manager, cmd, entities);
}
}
protected virtual void UpdateRelations(TransactionManager manager, DbCommand cmd, params Entity[] entities)
{
UpdateRelations(manager, cmd, (IEnumerable
}
protected virtual void DeleteRelations(TransactionManager manager, DbCommand cmd, IEnumerable
{
if (keys == null) return;
foreach (EntityRelationDataProvider
{
provider.Delete(manager, cmd, keys);
}
}
protected virtual void DeleteRelations(TransactionManager manager, DbCommand cmd, params Key[] keys)
{
DeleteRelations(manager, cmd, (IEnumerable
}
protected virtual void DeleteRelations(TransactionManager manager, DbCommand cmd, string whereClause)
{
string whereCluase = ProcessWhereClause(whereClause);
foreach (EntityRelationDataProvider
{
provider.Delete(manager, cmd, whereClause);
}
}
protected virtual void CleanRelations(DbCommand cmd)
{
foreach (EntityRelationDataProvider
{
provider.Clean(cmd);
}
}
protected virtual void RetrieveRelations(TransactionManager manager, DbCommand cmd, IEnumerable
{
if (entities == null) return;
foreach (EntityRelationDataProvider
{
provider.Retrieve(manager, cmd, entities);
}
}
protected virtual void RetrieveRelations(TransactionManager manager, DbCommand cmd, params Entity[] entities)
{
RetrieveRelations(manager, cmd, (IEnumerable
}
#endregion
#region 基本操作方法
public abstract void Insert(TransactionManager manager, Entity entity);
public abstract void Import(TransactionManager manager, IEnumerable
public abstract void Update(TransactionManager manager, Entity entity);
public abstract void Delete(TransactionManager manager, Key key);
public abstract void DeleteAll(TransactionManager manager, string whereClause);
public abstract Entity Get(TransactionManager manager, Key key);
public virtual List
{
return GetPaged(manager, whereClause, orderBy, -1, -1);
}
public abstract List
public abstract int GetCount(TransactionManager manager, string whereClause);
public abstract long GetVersion(TransactionManager manager, string whereClause);
public abstract Entity Fill(IDataReader reader, Entity entity);
#endregion
#region 内部操作模板方法
protected virtual int InternalExecuteNonQuery(TransactionManager manager, string sql, params DbParameter[] parameters)
{
int result = 0;
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取SQL语句
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
//执行
result = cmd.ExecuteNonQuery();
/*------------------------------数据库SQL操作结束----------------------------------*/
});
return result;
}
protected virtual List
{
List
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取SQL语句
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
//执行获取实体集
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(Fill(reader, null));
}
}
//获取关系对象
RetrieveRelations(mgr, cmd, list);
/*------------------------------数据库SQL操作结束----------------------------------*/
});
return list;
}
protected virtual List
{
List
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取SQL语句
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
//执行获取实体集
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add((Key)Convert.ChangeType(reader.GetValue(0), typeof(Key)));
}
}
/*------------------------------数据库SQL操作结束----------------------------------*/
});
return list;
}
protected virtual T InternalGetValue
{
T val = default(T);
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取SQL语句
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
//执行获取实体集
object objValue = cmd.ExecuteScalar();
val = DbConvert.ChangeType
/*------------------------------数据库SQL操作结束----------------------------------*/
});
return val;
}
protected virtual List
{
List
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取SQL语句
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
//执行获取实体集
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(DbConvert.ChangeType
}
}
/*------------------------------数据库SQL操作结束----------------------------------*/
});
return list;
}
public delegate T FillEntityHandler
public delegate void AfterRetrievedEntityListHandler
protected T InternalGet
where T : class
{
if (fillEntityHandler == null) throw new ArgumentNullException("fillEntityHandler", "实体数据填充调用方法不能为空!");
T entity = null;
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取SQL语句
cmd.CommandText = sqlGet;
cmd.Parameters.AddRange(parameters);
//执行获取实体
using (IDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
entity = fillEntityHandler(reader, null);
}
}
if (afterRetrieveHandler != null && entity != null)
{
afterRetrieveHandler(mgr, cmd, new T[] { entity });
}
/*------------------------------数据库SQL操作结束----------------------------------*/
});
return entity;
}
protected Entity InternalGet(TransactionManager manager, string sqlGet, params DbParameter[] parameters)
{
return InternalGet
}
protected List
{
return InternalGetPaged
}
protected List
where T:class
{
if (fillEntityHandler == null) throw new ArgumentNullException("fillEntityHandler", "实体数据填充调用方法不能为空!");
List
int count = 0;
Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
{
/*------------------------------数据库SQL操作开始----------------------------------*/
//获取总记录数
cmd.CommandText = sqlGetCount;
cmd.Parameters.AddRange(parameters);
count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
//确定分页和获取记录的数量
if (pageIndex < 0 || pageSize <= 0)
{
list = new List
}
else if (count > (pageIndex-1) * pageSize)
{
list = new List
}
else
{
list = new List
return;
}
//获取记录集
cmd.CommandText = sqlGetPaged;
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(fillEntityHandler(reader, null));
}
}
if (afterRetrieveHandler != null)
{
afterRetrieveHandler(mgr, cmd, list);
}
}
else
{
list = new List
return;
}
/*------------------------------数据库SQL操作结束----------------------------------*/
});
totalCount = count;
return list;
}
#endregion
}
}