我正在遍历 C# 类的属性以将值与另一个实例进行比较。这个概念似乎很简单,并且适用于我正在尝试做的事情。但是,我的 foreach 循环永远不会停止。它只是继续循环遍历类并产生一个StackOverflowException
. 我对这个不知所措。任何帮助将不胜感激!
public static Object ORIGINALRECORD { get; set; }
protected String DirtySets()
{
String sDirtySets = "";
foreach (PropertyInfo property in this.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance))
{
if (ORIGINALRECORD.GetType() == this.GetType())
{
System.Diagnostics.Debug.WriteLine(property.Name);
object originalValue = ORIGINALRECORD.GetType().GetProperty(property.Name).GetValue(ORIGINALRECORD, null);
object newValue = property.GetValue(this, null);
if (!object.Equals(originalValue, newValue))
{
sDirtySets = (sDirtySets == "" ? "" : sDirtySets + ",") + property.Name + "=?";
}
}
}
return "SET "+sDirtySets;
}