Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Self reflection
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?


Report this thread to moderator Post Follow-up to this message
Old Post
Blaze1
05-06-05 01:58 PM


Re: Self reflection
> 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;
}



Report this thread to moderator Post Follow-up to this message
Old Post
Lorad
05-18-05 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:51 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.