Code Comments
Programming Forum and web based access to our favorite programming groups.I'm writing a base class for out business objects and one requirement
is that the object is able to store a snapshot of it's properties
before editing occurs so that it can revert to them in case of a
cancelled edit.
I thought that I could write a generic SaveState() and RevertState()
function that would use reflection to enumerate the properties and
assign them as needed. The problem is that I'm unsure how to reference
an object type of itself.
For example an implementation might look something like this (non
working code):
private void SaveState()
{
System.Type myType = this.GetType();
myType currState;
PropertyInfo[] props = myType.GetProperties();
foreach (PropertyInfo p in props)
{
currState.Property[p.Name] = thos.Property[p.Name]
}
}
this way, a single base method could be used for all derived classes.
Is this doable? Are there any other methods I could use to implement
this type of functionality?
Post Follow-up to this message> I'm writing a base class for out business objects and one requirement
> is that the object is able to store a snapshot of it's properties
> before editing occurs so that it can revert to them in case of a
> cancelled edit.
>
> I thought that I could write a generic SaveState() and RevertState()
> function that would use reflection to enumerate the properties and
> assign them as needed. The problem is that I'm unsure how to reference
> an object type of itself.
>
> For example an implementation might look something like this (non
> working code):
>
> private void SaveState()
> {
> System.Type myType = this.GetType();
> myType currState;
>
> PropertyInfo[] props = myType.GetProperties();
>
> foreach (PropertyInfo p in props)
> {
> currState.Property[p.Name] = thos.Property[p.Name]
> }
>
> }
>
> this way, a single base method could be used for all derived classes.
>
> Is this doable? Are there any other methods I could use to implement
> this type of functionality?
>
As others have said reflection is slower, but if you don't care about
execution speed........
// this base class will save all properties that have a get & set and
restore these properties at a later time.
// note if any of the properties save classes the class will NOT be a clone
it will only be a reference to that type
// it is left as an exercise to the reader to fix this for clonable types.
// you could change this to save member data
public class BaseCopy
{
public BaseCopy(){ }
public void SaveState()
{
_values = new System.Collections.Hashtable();
System.Type t = this.GetType();
foreach(System.Reflection.PropertyInfo pi in t.GetProperties())
{
if ( pi.CanWrite && pi.CanRead )
{
_values.Add(pi, pi.GetValue(this, null));
}
}
}
public void RestoreState()
{
foreach(System.Reflection.PropertyInfo key in _values.Keys)
{
key.SetValue(this, _values[key], null);
}
}
private System.Collections.Hashtable _values;
}
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.