I have an Entity named Buseartag under namespace GDeerParkEntity, below is the schema:
namespace GDeerParkEntity
{
public class Buseartag
{
public Guid Eartag_id { get; set; } //pk
public String Eartag_code { get; set; }
public Nullable<Guid> Sex_id { get; set; }
public Nullable<Guid> Breed_id { get; set; }
public Nullable<Guid> Primarily_id { get; set; }
public Nullable<Guid> Bas_deerpen_id { get; set; }
public String Chip_num { get; set; }
public String Eartag_note { get; set; }
}
}
I also have another Entity named Busremove with Buseartag inside under the same namespace GDeerParkEntity:
namespace GDeerParkEntity
{
public class Busremove
{
public Guid Removeid { get; set; }
public Nullable<Guid> Eartagid { get; set; }
public string Removereason { get; set; }
public DateTime Removetime { get; set; }
public Guid Suppenid { get; set; }
public Guid Subpenid { get; set; }
public string Removenote { get; set; }
public Buseartag BuseartagModel { get; set; }
}
}
But now, in the client, I have another two entitys with the same names as above but namespace different.
namespace ServiceProxy
{
public class Buseartag
{
public Guid Eartag_id { get; set; } //pk
public String Eartag_code { get; set; }
public Nullable<Guid> Sex_id { get; set; }
public Nullable<Guid> Breed_id { get; set; }
public Nullable<Guid> Primarily_id { get; set; }
public Nullable<Guid> Bas_deerpen_id { get; set; }
public String Chip_num { get; set; }
public String Eartag_note { get; set; }
}
}
===================================================
namespace ServiceProxy
{
public class Busremove
{
public Guid Removeid { get; set; }
public Nullable<Guid> Eartagid { get; set; }
public string Removereason { get; set; }
public DateTime Removetime { get; set; }
public Guid Suppenid { get; set; }
public Guid Subpenid { get; set; }
public string Removenote { get; set; }
public Buseartag BuseartagModel { get; set; }
}
}
So I want to convert the GDeerParkEntity.Busremove entity to ServiceProxy.Busremove entity by using reflection (because there are a lot of entities like this should be converted, So I used T to cover these scenario):
public class Utils
{
public static T ConvertFromEntity<T, T1>(T1 t1)
{
if (t1 == null) return default(T);
Type type = typeof(T);
Type typeEx = typeof(T1);
PropertyInfo[] infoT = type.GetProperties();
PropertyInfo[] infoT1 = typeEx.GetProperties();
T t= Activator.CreateInstance<T>();
foreach (PropertyInfo pT in infoT)
{
string pTName = pT.Name;
foreach (PropertyInfo pT1 in infoT1)
{
try
{
if (pT1.Name.Equals(pTName))
{
if (!pT1.Name.ToLower().Contains("model"))
{
object pT1Value = pT1.GetValue(t1, null);
pT.SetValue(t as object, pT1Value, null);
break;
}
else
{
//How should I do here to convert Buseartag?
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
return t;
}
The usage is like below:
ServiceProxy.Busremove bus = Utils.ConvertFromEntity<ServiceProxy.Busremove, GDeerParkEntity.Busremove>(remove);
But now I have diffcult in converting Buseartag inside Busremove entity, anyone can help me ? thx. I have tried type.getnestedtypes() but it didn't work for me.