'is there a clean way of getting field name in c#
I have a method to copy over fields from one object to another
public static T CopyEntity<T>(this T source, T other)
{
if (source.GetType() != other.GetType()) return default(T);
var fields = source.GetType()
.GetProperties()
.Where(x => x.Name.ToUpper().CompareTo("ID") != 0).ToList();
foreach (var field in fields)
{
field.SetValue(source,field.GetValue(other));
}
return source;
}
there is the part where I filter out the id field because I would never want to copy that over. What I want is to add optional parameters to filter other fields I may want to add on the fly, I know I could use something like
obj.CopyEntity<OBJ>(other,nameof(obj.foo));
is there a way to do it simply like
obj.CopyEntity<OBJ>(other,obj.foo);
and parse the field name in the method so I dont have to add nameof for every thing I want the function to ignore?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
