what he wants to say is
if you have two classes which shares most of the properties can you cast an object from class a to class b and automatically make the system understand the assignment by shared properties names ?
option1: use reflection
disadvantage : gonna slow you down more than you think.
option2: make one derive from another and have one with common properties and other extend that
disadvantage: coupled ! if your doing that for 2 layers in your application then the 2 layers will be coupled
not sure if that what you mean !! correct me if im wrong
Let there be :
class customer
{
public string firstname { get; set; }
public string lastname { get; set; }
public int age { get; set; }
}
class employee
{
public string firstname { get; set; }
public int age { get; set; }
}
now here is an extension for Object type
public static T Cast<T>(this Object myobj)
{
Type objectType = myobj.GetType();
Type target = typeof(T);
var x = Activator.CreateInstance(target, false);
var z = from source in objectType.GetMembers().ToList() where source.MemberType == MemberTypes.Property select source ;
var d = from source in target.GetMembers().ToList() where source.MemberType == MemberTypes.Property select source;
List<MemberInfo> members = d.Where(memberInfo => d.Select(c => c.Name).ToList().Contains(memberInfo.Name)).ToList();
PropertyInfo propertyInfo;
object value;
foreach (var memberInfo in members)
{
propertyInfo = typeof(T).GetProperty(memberInfo.Name);
value = myobj.GetType().GetProperty(memberInfo.Name).GetValue(myobj,null);
propertyInfo.SetValue(x,value,null);
}
return (T)x;
}
now you use it like this :
static void Main(string[] args)
{
var cus = new customer();
cus.firstname = "John";
cus.age = 3;
employee emp = cus.Cast<employee>();
}
method cast check common properties between two object and do the assignment and return new object all ready.