我有以下代码,它接受两个对象并返回可能已更改的属性列表:
public static List<ConfirmRow> EnumerateFormDiffs(object dbForm, object webForm)
{
var output = new List<ConfirmRow>();
var type = dbForm.GetType();
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var property in propertyInfos)
{
Type propertyType = property.GetType();
if (!propertyType.IsPrimitive && !propertyType.Equals(typeof(string)) && !propertyType.Equals(typeof(DateTime)))
{
continue;
}
var oldVal = property.GetValue(dbForm, null);
var newVal = property.GetValue(webForm, null);
if (oldVal != null && newVal != null)
{
if (oldVal.ToString() != newVal.ToString())
{
var displayName = (ModelMetadata.FromStringExpression(property.Name, new ViewDataDictionary(dbForm)).DisplayName ?? property.Name);
var tmp = new ConfirmRow
{
FieldName = displayName,
OldValue = DisplayField(property.Name, type.Name, oldVal),
NewValue = DisplayField(property.Name, type.Name, newVal)
};
output.Add(tmp);
}
}
else if (oldVal == null && newVal != null)
{
var displayName = (ModelMetadata.FromStringExpression(property.Name, new ViewDataDictionary(dbForm)).DisplayName ?? property.Name);
var tmp = new ConfirmRow { FieldName = displayName, OldValue = "", NewValue = DisplayField(property.Name, type.Name, newVal) };
output.Add(tmp);
}
else if (newVal == null && oldVal != null)
{
var displayName = (ModelMetadata.FromStringExpression(property.Name, new ViewDataDictionary(dbForm)).DisplayName ?? property.Name);
var tmp = new ConfirmRow { FieldName = displayName, OldValue = DisplayField(property.Name, type.Name, oldVal), NewValue = "" };
output.Add(tmp);
}
}
return output;
}
我正在比较两个“注册”对象:
public class Enrollment
{
public int Id { get; set; }
public int? ClientId { get; set; }
[ForeignKey("ClientId")]
public virtual Client Client { get; set; }
public int Version { get; set; }
.
.
.
public SaveSubmitStatus? SaveSubmitStatus { get; set; }
}
其中 SaveSubmitStatus 是一个枚举。
现在,我想告诉我的代码不要查看“客户端”字段,因为这会导致问题,但我确实希望它查看“SaveSubmitStatus”字段何时更改。当我调试它时,在这两个属性上,PropertyType 都是“RuntimePropertyInfo”。谁能告诉我如何确定并包含在我的 if 语句中,以查看“SaveSubmitStatus”字段的差异?调试时我在对象中找不到任何东西,以便我确定这一点。