Как динамически копировать основную коллекцию подробностей в silverlight С#

Я хочу создать динамический метод, который будет глубоко копировать сущность (сущность может содержать сложный тип). Как обычно, как c#.net. Я пробовал в С#, тогда система работает правильно. Но когда я хочу выполнить глубокое копирование в Silverlight, система отправляет мне сообщение об ошибке (метод набора свойств не найден в Silverlight). N.B.: Тогда я увидел очень тонкую разницу между C# и Silverlight. Я нашел разницу, которая заключается в том, что свойство «запись» ложно. В этом случае что мне делать? Пример кода:

[Serializable]
public class Department
{        [Key]
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
        public int EmployeeId { get; set; }
}


  [Serializable]
    public class Employee   
    {
        [Key]
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }

        [Include]
        [Composition]
        [Association("DC_Receive_Department", "EmployeeId", "EmployeeId")]
        private List<Department> _DepartmentList = new List<Department>();
        public List<Department> DepartmentList
        {
            get { return _DepartmentList; }
            set { _DepartmentList = value; }
        }
    }


 public static class DictionaryExt
    {
        public static T MyDeepCopy<T>(this T source)
        {
            try
            {

                //Throw if passed object has nothing
                if (source == null) { throw new Exception("Null Object cannot be cloned"); }

                // Don't serialize a null object, simply return the default for that object
                if (Object.ReferenceEquals(source, null))
                {
                    return default(T);
                }

                //variable declaration
                T copy;
                var obj = new DataContractSerializer(typeof(T));
                using (var memStream = new MemoryStream())
                {
                    obj.WriteObject(memStream, source);
                    memStream.Seek(0, SeekOrigin.Begin);
                    copy = (T)obj.ReadObject(memStream);
                }
                return copy;
            }
            catch (Exception)
            {
                throw;
            }
        }


        public static ObservableCollection<T> DeepCopyMasterDetail<T>(this ObservableCollection<T> entityCollection)
        {
            try
            {
                Type t = entityCollection.GetType();
                ObservableCollection<T> RooTList = new ObservableCollection<T>();

                foreach (T objEntity in entityCollection)
                {
                    //var separateMaster = objEntity.MyDeepCopy();
                    //RooTList.Add(separateMaster);
                    T iObject = CreateMaster(objEntity);
                    RooTList.Add(iObject);
                }
                return RooTList;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }


        private static dynamic CreateMaster<T>(this T objSource)
        {
            //Get the type of source object and create a new instance of that type
            Type typeSource = objSource.GetType();
            object objTarget = Activator.CreateInstance(typeSource);
            //Get all the properties of source object type
            PropertyInfo[] propertyInfo = typeSource.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            objTarget = objSource.MyDeepCopy();
            //Assign all source property to taget object 's properties
            foreach (PropertyInfo property in propertyInfo)
            {
                //Check whether property can be written to 
                //if (property.CanWrite)
                //{
                //check whether property type is value type, enum or string type
                if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>))
                {
                    Type t1 = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                    dynamic safeValue1 = (property.GetValue(objSource, null) == null) ? null : Convert.ChangeType(property.GetValue(objSource, null), t1, null);

                    object list = null;

                    foreach (dynamic item in safeValue1)
                    {
                        if (list == null)
                        {
                            list = BuildListHelper(item);
                        }
                        ((dynamic)list).Add(CreateMaster(item));

                    }
                    if (list != null)
                    {
                        property.SetValue(objTarget, list, null);
                    }

                }
            }
            return objTarget;
        }


        public static ObservableCollection<T> BuildListHelper<T>(this T item)
        {
            ObservableCollection<T> list = new ObservableCollection<T>();
            return list;
        }

    }

person Community    schedule 10.02.2014    source источник