我创建了一个哈希表来缓存反射结果。第一次,需要调用 GetProperties 并将结果存储到 hastable 中。下一次,首先检查 List of PropertyInfo 对象的哈希表。如果存在,请使用它。如果没有,请调用 GetProperties。
我使用它来将数据读取器映射到实体列表。
我的实现基于:A Defense on Reflection in .Net,作者 Nick Harrison (http://www.simple-talk.com/dotnet/.net-framework/a-defense-of-reflection-in-.net/)。
原来如此:
public class MapeadorDataReaderListaObjetos
{
private Hashtable properties;
private Hashtable Properties
{
get
{
if (properties == null)
properties = new Hashtable();
return properties;
}
set { properties = value; }
}
private void LoadProperties(object targetObject, Type targetType)
{
var flags = BindingFlags.DeclaredOnly| BindingFlags.Instance| BindingFlags.Public;
if (properties == null)
{
List<PropertyInfo> propertyList = new List<PropertyInfo>();
PropertyInfo[] objectProperties = targetType.GetProperties(flags);
foreach (PropertyInfo currentProperty in objectProperties)
{
propertyList.Add(currentProperty);
}
properties = new Hashtable();
properties[targetType.FullName] = propertyList;
}
if (properties[targetType.FullName] == null)
{
List<PropertyInfo> propertyList = new List<PropertyInfo>();
PropertyInfo[] objectProperties = targetType.GetProperties(flags);
foreach (PropertyInfo currentProperty in objectProperties)
{
propertyList.Add(currentProperty);
}
properties[targetType.FullName] = propertyList;
}
}
public void MapearDataReaderListaObjetos <T> (IDataReader dr, List<T> lista) where T: new()
{
Type businessEntityType = typeof(T);
List<T> entitys = new List<T>();
T miObjeto = new T();
LoadProperties(miObjeto, businessEntityType);
List<PropertyInfo> sourcePoperties = Properties[businessEntityType.FullName] as List<PropertyInfo>;
while (dr.Read())
{
T newObject = new T();
for (int index = 0; index < dr.FieldCount; index++)
{
for (int _indice = 0; _indice < sourcePoperties.Count; _indice++)
{
if (sourcePoperties[_indice].Name.ToUpper() == dr.GetName(index).ToUpper());
{
string _tipoProp = sourcePoperties[_indice].PropertyType.ToString();
PropertyInfo info = sourcePoperties[_indice] as PropertyInfo;
if ((info != null) && info.CanWrite)
{
info.SetValue(newObject, dr.GetValue(index), null);
}
}
}
}
entitys.Add(newObject);
}
dr.Close();
lista = entitys;
}
}
然后,我从我的 DataAcces 层调用它,如下所示:
public List <Entities.ENFactura> ListaxIdFactura (SqlTransaction Tr, Entities.ENFactura oBEFactura)
{
SqlConnection Cn = new SqlConnection();
Cn = _Connection.ConexionSEG();
List<Entities.ENFactura> loBEFactura = new List<Entities.ENFactura>();
using (Cn)
{
Cn.Open();
SqlDataReader drd = (odaSQL.fSelDrd(Cn, Tr, "Pa_CC_Factura_Listar_x_IdProveedor", oBEFactura));
if (drd != null)
{
if (drd.HasRows)
{
mapeador.MapearDataReaderListaObjetos <ENFactura>(drd, loBEFactura);
}
}
}
return (loBEFactura);
}
因此,通过这种方式,DAL 获得了一个数据读取器,将其映射到业务实体列表,并将其返回给业务逻辑层。
这个类 (MapeadorDataReaderListaObjetos) 仍然存在一些问题,尤其是在:
info.SetValue(newObject, _valor, null);
newObject 和 _valor 必须是相同的类型,否则你会得到一个异常(从 System.Int64 转换到 System.Int32,如果你的实体属性是 Int32 并且它在数据库表中的对应字段是 bigint,例如) .
另外,如果一个实体属性是另一个实体,这将不起作用,因为数据读取器不返回实体对象。
显然,这可以改进。
关于反射和委托,我发现这篇文章:反射 - 慢还是快?解决方案演示,作者 Abhishek Sur,在
http://www.abhisheksur.com/2010/11/reflection-slow-or-faster-demonstration.html
另一篇好文章是:Dodge Common Performance Pitfalls to Craft Speedy Applications,作者 Joel Pobar,http://msdn.microsoft.com/en-us/magazine/cc163759.aspx。
希望这会有所帮助。