asign only common fields in c# object
private Func<S,T> AutoMapFactory<S,T>() where T: class, new() where S : class
{
List<Action<T, S>> mapActions = typeof(T).GetProperties().Where(tp => tp.CanWrite)
.SelectMany(tp => typeof(S).GetProperties().Where(sp => sp.CanRead)
.Where(sp => sp.Name == tp.Name && tp.PropertyType.IsAssignableFrom(sp.PropertyType))
.Select(sp => (Action<T,S>)((targetObj, sourceObj) =>
tp.SetValue(targetObj, sp.GetValue(sourceObj)))))
.ToList();
return sourceObj => {
if (sourceObj == null) return null;
T targetObj = new T();
mapActions.ForEach(action => action(targetObj, sourceObj));
return targetObj;
};
}
Also in C#:
- c# max two values
- asp.net core mvc not triggering client side validation
- how to convert int to string unity c#
- how to reduce garbage collection c#
- unity how to change text in script
- 2d movement unity
- asign only common fields in c# object
- c# two dimensional array
- test how catch exception c#
- rotatearound unity
- unity line renderer position count
- covert char[] to string C#
- how to run csharp in visual studio code
- c# webcam
- how to destroy a gameobject after some hits in unity 3d
- Int64
- c# docstring
- c# declare an int list
- how to get the last element in an array in c#
- remove first object from list c#
- attribute usage c#
- lazy loading c#
- enums c#
- xarray: create 2d dataset