जॉन स्कीट और मार्क ग्रेवेल के पास MiscUtil नामक लाइब्रेरी है। MiscUtil.Reflection
के अंदर PropertyCopy
नामक एक कक्षा है जो आप वास्तव में वर्णन करते हैं। यह केवल .NET 3.5 के लिए काम करता है।
यह सोर्सटाइप के सार्वजनिक गुणों पर चलकर काम करता है, लक्ष्य टाईप के सार्वजनिक गुणों के साथ उन्हें नाम से मेल खाता है, यह सुनिश्चित करता है कि प्रत्येक संपत्ति को स्रोत से लक्ष्य में असाइन किया जा सके और फिर एक कॉपीियर को कैश और कैश कर सकें उन दो प्रकार के लिए कार्य करें (इसलिए आप हर बार यह प्रतिबिंब नहीं करते हैं)। मैंने इसे उत्पादन कोड में उपयोग किया है और इसकी भलाई के लिए प्रतिबद्ध हो सकता है।
अरे, मुझे लगा कि मैं सिर्फ उनके संक्षिप्त कोड पोस्ट करूंगा (यह 100 से कम लाइनों/टिप्पणियों से कम है)। इस कोड के लिए लाइसेंस here पाया जा सकता है:
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
namespace MiscUtil.Reflection
{
/// <summary>
/// Generic class which copies to its target type from a source
/// type specified in the Copy method. The types are specified
/// separately to take advantage of type inference on generic
/// method arguments.
/// </summary>
public static class PropertyCopy<TTarget> where TTarget : class, new()
{
/// <summary>
/// Copies all readable properties from the source to a new instance
/// of TTarget.
/// </summary>
public static TTarget CopyFrom<TSource>(TSource source) where TSource : class
{
return PropertyCopier<TSource>.Copy(source);
}
/// <summary>
/// Static class to efficiently store the compiled delegate which can
/// do the copying. We need a bit of work to ensure that exceptions are
/// appropriately propagated, as the exception is generated at type initialization
/// time, but we wish it to be thrown as an ArgumentException.
/// </summary>
private static class PropertyCopier<TSource> where TSource : class
{
private static readonly Func<TSource, TTarget> copier;
private static readonly Exception initializationException;
internal static TTarget Copy(TSource source)
{
if (initializationException != null)
{
throw initializationException;
}
if (source == null)
{
throw new ArgumentNullException("source");
}
return copier(source);
}
static PropertyCopier()
{
try
{
copier = BuildCopier();
initializationException = null;
}
catch (Exception e)
{
copier = null;
initializationException = e;
}
}
private static Func<TSource, TTarget> BuildCopier()
{
ParameterExpression sourceParameter = Expression.Parameter(typeof(TSource), "source");
var bindings = new List<MemberBinding>();
foreach (PropertyInfo sourceProperty in typeof(TSource).GetProperties())
{
if (!sourceProperty.CanRead)
{
continue;
}
PropertyInfo targetProperty = typeof(TTarget).GetProperty(sourceProperty.Name);
if (targetProperty == null)
{
throw new ArgumentException("Property " + sourceProperty.Name + " is not present and accessible in " + typeof(TTarget).FullName);
}
if (!targetProperty.CanWrite)
{
throw new ArgumentException("Property " + sourceProperty.Name + " is not writable in " + typeof(TTarget).FullName);
}
if (!targetProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
{
throw new ArgumentException("Property " + sourceProperty.Name + " has an incompatible type in " + typeof(TTarget).FullName);
}
bindings.Add(Expression.Bind(targetProperty, Expression.Property(sourceParameter, sourceProperty)));
}
Expression initializer = Expression.MemberInit(Expression.New(typeof(TTarget)), bindings);
return Expression.Lambda<Func<TSource,TTarget>>(initializer, sourceParameter).Compile();
}
}
}
}
स्रोत
2010-04-12 19:38:38
बस रिकॉर्ड के लिए - स्वीकार किए गए उत्तर में दूसरों की तुलना में अधिक ओवरहेड होंगे, क्योंकि यह कच्चे प्रतिबिंब का उपयोग करता है। 'ऑटोमैपर' और 'एक्सप्रेशन' जैसे दृष्टिकोण रनटाइम पर आईएल में इसे पूर्व-संकलित करते हैं, जो * महत्वपूर्ण * प्रदर्शन फायदे उत्पन्न कर सकता है। तो यदि आप इनमें से बहुत कुछ कर रहे हैं, तो शायद मूल प्रतिबिंब दृष्टिकोण से बचें। –