2010-05-07 73 views
3

我有一個單獨的類爲我的每個數據庫實體,當我創建我的類的對象引用類的屬性時,它返回一個循環引用,其中包含其他實體的屬性通過FK相關...刪除循環引用我想先通過「上下文代理對象」複製對象的副本,然後獲取該對象的基本類型,複雜類型,arrayEnumerable類型,並從對象中去除這些類型然後通過Web服務返回對象....獲取原始,複雜,ArrayEnumerable類型

+2

我建議你寫一些你想要實現的代碼示例。您對問題的描述不夠清楚(至少對我而言)。 – 2010-05-09 08:26:16

回答

0

聽起來像一個遞歸淺層克隆。我已經使用了以下內容,但只有一個級別。

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; 
    } 

}