通用更改跟踪 class 改进
Generic change tracking class improvements
我正在开发一个通用反射 class 用于更改跟踪。我所拥有的对于我完成的所有 classes 都很好用。我正准备将其作为工具的一部分移至整个团队。在我将它展示给每个人之前,我有兴趣进一步改进它。它是从具有错误处理的方法调用的,因此该部分不是问题。这在我们的逻辑中也很完美,就像我们将对象展平以进行更改跟踪一样,但是我是否遗漏了一些可能成为问题的东西,即使它在通常情况下很完美。
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;
}
}
具体来说这是我的问题。
有没有更好的方法来设置 propA 和 propB 来提高性能?它适用于一个对象的变化,我已经测试了多达 103 个属性,没有性能问题,但我尽量避免这样的事情。
谢谢
吉米
您可以使用反射 + Expression Trees 组合来构建 getter Func
。我建议在应用程序启动时构建此表达式并缓存它们(按类型),这应该会大大提高性能。但这会大大增加你的代码库大小=)
我正在开发一个通用反射 class 用于更改跟踪。我所拥有的对于我完成的所有 classes 都很好用。我正准备将其作为工具的一部分移至整个团队。在我将它展示给每个人之前,我有兴趣进一步改进它。它是从具有错误处理的方法调用的,因此该部分不是问题。这在我们的逻辑中也很完美,就像我们将对象展平以进行更改跟踪一样,但是我是否遗漏了一些可能成为问题的东西,即使它在通常情况下很完美。
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;
}
}
具体来说这是我的问题。
有没有更好的方法来设置 propA 和 propB 来提高性能?它适用于一个对象的变化,我已经测试了多达 103 个属性,没有性能问题,但我尽量避免这样的事情。
谢谢 吉米
您可以使用反射 + Expression Trees 组合来构建 getter Func
。我建议在应用程序启动时构建此表达式并缓存它们(按类型),这应该会大大提高性能。但这会大大增加你的代码库大小=)