3
我有一个单独的类为我的每个数据库实体,当我创建我的类的对象引用类的属性时,它返回一个循环引用,其中包含其他实体的属性通过FK相关...删除循环引用我想先通过“上下文代理对象”复制对象的副本,然后获取该对象的基本类型,复杂类型,arrayEnumerable类型,并从对象中去除这些类型然后通过Web服务返回对象....获取原始,复杂,ArrayEnumerable类型
我有一个单独的类为我的每个数据库实体,当我创建我的类的对象引用类的属性时,它返回一个循环引用,其中包含其他实体的属性通过FK相关...删除循环引用我想先通过“上下文代理对象”复制对象的副本,然后获取该对象的基本类型,复杂类型,arrayEnumerable类型,并从对象中去除这些类型然后通过Web服务返回对象....获取原始,复杂,ArrayEnumerable类型
听起来像一个递归浅层克隆。我已经使用了以下内容,但只有一个级别。
public static class EntityBaseExtensions
{
/// <summary>
/// Description: Creates a non-recursive shallow copy of an entity, only including public instance properties decorated with ColumnAttribute.
/// This will return an object without entity references.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns>A non-recursive shallow copy of a LINQ entity</returns>
public static T ShallowClone<T>(this T source) where T : EntityBaseClass
{
// create an object to copy values into
T destination = Activator.CreateInstance<T>();
// get source and destination property infos for all public instance
PropertyInfo[] sourcePropInfos = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] destinationPropInfos = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo sourcePropInfo in sourcePropInfos)
{
if (Attribute.GetCustomAttribute(sourcePropInfo, typeof(ColumnAttribute), false) != null)
{
PropertyInfo destPropInfo = destinationPropInfos.Where(pi => pi.Name == sourcePropInfo.Name).First();
destPropInfo.SetValue(destination, sourcePropInfo.GetValue(source, null), null);
}
}
return destination;
}
}
我建议你写一些你想要实现的代码示例。您对问题的描述不够清楚(至少对我而言)。 – 2010-05-09 08:26:16