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-03-05 01:58 AM


Re: Self reflection
What it looks like you want is a Clone method. Look at the ICloneable
interface.

If you want a good, general way of implementing Clone() that takes
inheritance into account, try this:

// If your type doesn't have a "no arguments" constructor then make a
protected one
// that doesn't do anything, and document the fact that it's for use
only in the Clone()
// method.
protected MyType()
{  }

public MyType Clone()
{
MyType result = new MyType();
this.CopyTo(result);
return result;
}

protected CopyTo(MyType other)
{
// If you're inheriting from another one of your classes that has
// a CopyTo method, then you need to say
// base.CopyTo(other);
// here.
other.field1 = this.field1;
other.field2 = this.field2;
..
}

I wouldn't bother with a generic solution using Reflection. First of
all, Reflection comes with a performance penalty. Second, you'll leave
other programmers scratching their heads... ICloneable is a familiar
idiom that everyone will understand, even if it means writing code in
every class.


Report this thread to moderator Post Follow-up to this message
Old Post
Bruce Wood
05-03-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:38 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.