I am developing a generic reflection class for change tracking. What I have works just fine for all classes that I put it through. I am getting ready to move it out as part of a tool to the entire group. I am interested in improving this a bit more before I role it out to everyone. It is called from methods that have error handling in place so that part is not an issue. Also this works perfectly in our logic the way we flatten objects for change tracking, but am I missing something that could be an issue even though it works perfectly for the usual situations.
public class ChangeTracker
{
public static string GetChangesString<T,S>(T original, T current, S dto, string[] exluded)
{
StringBuilder sb = new StringBuilder();
PropertyInfo[] names = typeof(S).GetProperties();
string displayName = string.Empty;
foreach (PropertyInfo item in names)
{
if (exluded.Contains(item.Name)) continue;
//method that sets display name to either the property name or the display attribute if present
displayName = GetDisplayName(item);
object propA = original.GetType().GetProperty(item.Name).GetValue(original, null);
object propB = current.GetType().GetProperty(item.Name).GetValue(original, null);
if (propA == null && propB == null) continue;
if (propA == null && propB != null)
{
//appendline for value added
}
else if (propB == null && propA != null)
{
//appendline for value removed
}
else if (propA.ToString() != propB.ToString())
{
//appendline for value changed
}
}
return sb.ToString();
}
private static string GetDisplayName(PropertyInfo prop)
{
string display = string.Empty;
//Check for displayattribute and set correct name
return display;
}
}
Specifically these are my questions.
Can I put a .Where on the GetProperties that would take a lamda that would avoid the excluded.Contains check?
Is there something better that I can do on the propA and propB setting to improve performance? It works on one object changing and I have tested it up to 103 properties with no performance issues, but I try to avoid stuff like this when I can.
Are there issues that I am not seeing because the way we flatten our objects that would cause an issue if we change our design practices in the future?
Is there a way to shorten this down a little more? For a bigger project this will be a huge help, but for smaller change tracking this seems clunky.
Thanks Jimmy
Aucun commentaire:
Enregistrer un commentaire