I want to crate a dynamic method which will deep copy an entity(Entity may contain complex type). As usual like c#.net. I have tried in c# then system works properly. But when I want to deep copy in Silverlight then system send me an error(property set method not found silverlight).
N.B: Then I have seen very careful difference between C# and Silverlight. I have found a difference which is - property 'write' is false. In this case what shoud I do?
Code Sample:
[Serializable]
public class Department
{ [Key]
public int DepartmentId { get; set; }
public string DepartmentName { 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;
}
}