In C # and .NET 4.5 there is no built-in equivalent of $ .extend.
However, you can find many examples of people trying to achieve this behavior using reflection.NET. There are others that use serialization (JSON.NET, etc.) to achieve similar behavior. Another approach would be to use IOC containers such as Automapper .
, , Automapper IOC:
var expr = Mapper.CreateMap<ContactLstModel, ContactLstModel>().ForMember("ContactType", (conf) => { conf.Ignore(); });
var merged = Mapper.Map<ContactLstModel, ContactLstModel>(_Contact, _ContactOption);
Automapper .
, Reflection.
, , CopyValues, , .
CopyValues<ContactLstModel>(_Contact, _ContactOption);
, ContactType .
CopyValues :
public static void CopyValues<T>(T target, T source)
{
Type t = typeof(T);
var properties = t.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
foreach (var prop in properties)
{
var value = prop.GetValue(source, null);
if (value != null)
prop.SetValue(target, value, null);
}
}
, , jquery extend (, ..), . .
, # , Javascript, #, Javascript for-in Object.keys().