मैंने प्रतिबिंब परिणामों को कैश करने के लिए एक हैशटेबल बनाया है। पहली बार, GetProperties को कॉल करने और परिणामों को जल्दबाजी में स्टोर करने के लिए यह आवश्यक नहीं है। अगली बार, पहले PropertyInfo ऑब्जेक्ट्स की सूची के लिए हैशटेबल की जांच करें। यदि मौजूद है, तो इसका इस्तेमाल करें। यदि नहीं, GetProperties का आह्वान करें।
मैं इसका उपयोग एक डाटाएडर को सूची की सूची में मैप करने के लिए करता हूं।
मेरा कार्यान्वयन निक हैरिसन (http://www.simple-talk.com/dotnet/.net-framework/a-defense-of-reflection-in-.net/) द्वारा A Defense on 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);
}
तो, इस तरह से, दाल किसी DataReader हो जाता है, यह नक्शा व्यापार संस्थाओं की एक सूची में, और इसे व्यापार तर्क परत पर वापस कर दें।
इस वर्ग (MapeadorDataReaderListaObjetos), अभी भी कुछ मुद्दे हैं, विशेष रूप से पर:
info.SetValue(newObject, _valor, null);
newObject और _valor एक ही प्रकार का होना चाहिए या आप एक अपवाद (System.Int32 को System.Int64 से रूपांतरण मिल जाएगा, यदि आपकी इकाई संपत्ति Int32 है और डेटाबेस तालिका में इसके संबंधित फ़ील्ड को बड़ा है, उदाहरण के लिए)।
इसके अलावा, यदि एक इकाई संपत्ति एक और इकाई है, तो यह काम नहीं करेगा, क्योंकि डेटरेडर इकाई वस्तुओं को वापस नहीं करते हैं।
जाहिर है, यह बेहतर किया जा सकता है।
प्रतिबिंब और प्रतिनिधियों के संबंध में, मुझे यह आलेख मिला: प्रतिबिंब - धीमा या तेज़? समाधान, अभिषेक सुर से साथ प्रदर्शन, http://www.abhisheksur.com/2010/11/reflection-slow-or-faster-demonstration.html
एक और अच्छा लेख पर है: चकमा आम प्रदर्शन नुकसान शीघ्र आवेदन में कांट-छांट करने के लिए, जोएल Pobar द्वारा, http://msdn.microsoft.com/en-us/magazine/cc163759.aspx पर।
उम्मीद है कि इससे मदद मिलती है।
आपका उत्तर मुझे सही दिशा में इंगित करता है (PropertyInfo कैश कैसे करें) जो वास्तव में GetProperty से तेज़ है (अभी तक यह समय नहीं है, लेकिन मेरे पृष्ठ की प्रतिक्रिया में सुधार हुआ है।) – Alex
यदि आप संपत्ति को कॉल करने के लिए प्रतिनिधियों को कैश कर सकते हैं प्रकार-सुरक्षित तरीका, जो इसे फिर से तेज़ी से बना सकता है। निश्चित रूप से यह समय हालांकि ... –