Property-based shallow clones of value types are now also working correctly in the expression tree cloner
git-svn-id: file:///srv/devel/repo-conversion/nusu@247 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
d091061baa
commit
48e1674912
|
@ -157,8 +157,6 @@ namespace Nuclex.Support.Cloning {
|
||||||
// won't be last in the block when we're finished
|
// won't be last in the block when we're finished
|
||||||
ParameterExpression clone = Expression.Variable(clonedType);
|
ParameterExpression clone = Expression.Variable(clonedType);
|
||||||
variables.Add(clone);
|
variables.Add(clone);
|
||||||
|
|
||||||
// Give it a new instance of the type being cloned
|
|
||||||
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
|
transferExpressions.Add(Expression.Assign(clone, Expression.New(clonedType)));
|
||||||
|
|
||||||
// To access the properties of the original type, we need it to be of the actual
|
// To access the properties of the original type, we need it to be of the actual
|
||||||
|
@ -169,21 +167,9 @@ namespace Nuclex.Support.Cloning {
|
||||||
Expression.Assign(typedOriginal, Expression.Convert(original, clonedType))
|
Expression.Assign(typedOriginal, Expression.Convert(original, clonedType))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Enumerate all of the type's properties and generate transfer expressions for each
|
generateShallowPropertyBasedComplexCloneExpressions(
|
||||||
PropertyInfo[] propertyInfos = clonedType.GetProperties(
|
clonedType, typedOriginal, clone, transferExpressions, variables
|
||||||
BindingFlags.Public | BindingFlags.NonPublic |
|
|
||||||
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
|
||||||
);
|
);
|
||||||
for(int index = 0; index < propertyInfos.Length; ++index) {
|
|
||||||
PropertyInfo propertyInfo = propertyInfos[index];
|
|
||||||
|
|
||||||
transferExpressions.Add(
|
|
||||||
Expression.Assign(
|
|
||||||
Expression.Property(clone, propertyInfo),
|
|
||||||
Expression.Property(typedOriginal, propertyInfo)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure the clone is the last thing in the block to set the return value
|
// Make sure the clone is the last thing in the block to set the return value
|
||||||
transferExpressions.Add(clone);
|
transferExpressions.Add(clone);
|
||||||
|
@ -205,6 +191,70 @@ namespace Nuclex.Support.Cloning {
|
||||||
return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile();
|
return Expression.Lambda<Func<object, object>>(resultExpression, original).Compile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generates expressions to transfer the properties of a complex value type
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="clonedType">Complex value type that will be cloned</param>
|
||||||
|
/// <param name="original">Original instance whose properties will be cloned</param>
|
||||||
|
/// <param name="clone">Target instance into which the properties will be copied</param>
|
||||||
|
/// <param name="transferExpressions">Receives the value transfer expressions</param>
|
||||||
|
/// <param name="variables">Receives temporary variables used during the clone</param>
|
||||||
|
private static void generateShallowPropertyBasedComplexCloneExpressions(
|
||||||
|
Type clonedType,
|
||||||
|
ParameterExpression original,
|
||||||
|
ParameterExpression clone,
|
||||||
|
ICollection<Expression> transferExpressions,
|
||||||
|
ICollection<ParameterExpression> variables
|
||||||
|
) {
|
||||||
|
// Enumerate all of the type's properties and generate transfer expressions for each
|
||||||
|
PropertyInfo[] propertyInfos = clonedType.GetProperties(
|
||||||
|
BindingFlags.Public | BindingFlags.NonPublic |
|
||||||
|
BindingFlags.Instance | BindingFlags.FlattenHierarchy
|
||||||
|
);
|
||||||
|
for(int index = 0; index < propertyInfos.Length; ++index) {
|
||||||
|
PropertyInfo propertyInfo = propertyInfos[index];
|
||||||
|
Type propertyType = propertyInfo.PropertyType;
|
||||||
|
|
||||||
|
if(propertyType.IsPrimitive || (propertyType == typeof(string))) {
|
||||||
|
transferExpressions.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.Property(clone, propertyInfo),
|
||||||
|
Expression.Property(original, propertyInfo)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else if(propertyType.IsValueType) {
|
||||||
|
ParameterExpression originalProperty = Expression.Variable(propertyType);
|
||||||
|
variables.Add(originalProperty);
|
||||||
|
transferExpressions.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
originalProperty, Expression.Property(original, propertyInfo)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
ParameterExpression clonedProperty = Expression.Variable(propertyType);
|
||||||
|
variables.Add(clonedProperty);
|
||||||
|
transferExpressions.Add(
|
||||||
|
Expression.Assign(clonedProperty, Expression.New(propertyType))
|
||||||
|
);
|
||||||
|
|
||||||
|
generateShallowPropertyBasedComplexCloneExpressions(propertyType, originalProperty, clonedProperty, transferExpressions, variables);
|
||||||
|
|
||||||
|
transferExpressions.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.Property(clone, propertyInfo), clonedProperty
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
transferExpressions.Add(
|
||||||
|
Expression.Assign(
|
||||||
|
Expression.Property(clone, propertyInfo),
|
||||||
|
Expression.Property(original, propertyInfo)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates state transfer expressions to copy an array of primitive types
|
/// Generates state transfer expressions to copy an array of primitive types
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user