reflection - Stuck in an infinite loop iterating through properties in a C# class -


i'm iterating through properties of c# class compare values instance. concept seems simple , work i'm trying do. however, foreach loop never stops. continues loop through class , results in stackoverflowexception. i'm @ loss one. appreciated!

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; } 

inside loop have statement retrieves value of property on class:

object newvalue = property.getvalue(this, null); 

as long type of object not same type of originalrecord public property values retrieved.

if 1 of getters on 1 of properties calls dirtysets infinite recursion. inside loop call dirtysets starts new loop , on until stackoverflowexception.

to avoid need ensure either dirtysets not called getter of public property or don't retrieve values of properties calls dirtysets in getters.


Comments