Bueno, tengo realmente horrible manera en que podría hacerlo.
Podrías escribir un método que utilizara la reflexión (¡tengan paciencia!) Para calcular todas las propiedades para un tipo particular, y construí un delegado (usando Reflection.Emit) para copiar propiedades de ese tipo a otro. Luego use un tipo anónimo para asegurarse de que solo necesita construir el delegado de copia una vez, por lo que es rápido. Su método podría tener el siguiente aspecto:
public static Expression<Func<Foo, FooEditDto>> EditDtoSelector()
{
return f => MagicCopier<FooEditDto>.Copy(new {
f.PropertyA, f.PropertyB, f.PropertyC, f.PropertyD, f.PropertyC
});
}
Los matices aquí:
- MagicCopier es un tipo genérico y Copy es un método genérico de modo que se puede especificar explícitamente el tipo "blanco", pero implícitamente especifique el tipo de "fuente".
- Se trata de utilizar un inicializador de proyección para inferir los nombres de las propiedades de las expresiones utilizadas para inicializar el tipo anónimo
No estoy seguro de si es realmente vale la pena, pero es una idea bastante divertido ... Debo tener que implementarlo de todos modos :)
EDITAR: Con MemberInitExpression podríamos hacerlo todo con un árbol de expresiones, que lo hace mucho más fácil que CodeDOM. Lo intentaré esta noche ...
EDITAR: Hecho, y en realidad es un código bastante simple. Aquí está la clase:
/// <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();
}
}
Y llamándolo:
TargetType target = PropertyCopy<TargetType>.CopyFrom(new { First="Foo", Second="Bar" });
Ver la nota que agregué. FooEditDto es una subclase de FooDto. –