Dynamic casting to object base class

  • Thread starter Thread starter Zac
  • Start date Start date
Z

Zac

Alright anyone who has 2c throw it in...

I am working through a custom xml serializer and have come upon a
conundrum, given our class design.

The interface implemented on the base class (base for all business
entities) dictates that the implementing class expose a Dirty property
(for state).

The base class (that actually manages state, once for all inheritors)
maintains an array of Dirty (bools) and each class down the chain
knows (internally) the index in the array based on its level in the
heirarchy.

StateMgr (bool[] dirty)
- BaseEntity
- Person (dirty[0] = false)
- Employee (dirty[1] = false)
- Manager (dirty[2] = true)

Given the above, each entity overrides the Dirty property from the
base and via the override hides the indexing of the Dirty property (so
I could call manager.Dirty = true instead of manager.Dirty[2] = true).

Now in the serializer, I want to be able to serialize all of the
exposed properties PLUS the Dirty value for each of the entities in
the chain. In the resulting xml I would have something like <Manager
Dirty="true false false" ... indicating that only the properties in my
StateMgr need be updated.

Getting long - but stick with me. The Dirty property is exposed
through the interface so if I cast my Manager to an Employee the Dirty
property through that interface is false (given the above), and in
turn if I cast down to a Person then I also get the dirty property at
the Person index (0) which is false.

The problem is, I will only have "some object" when I serialize. I can
use the GetType().BaseType to see the base type - but I know of no way
to actually cast to that type (i.e. Employee tempObject = manager as
Employee) but using dyanmic properties like (Employee tempObject =
manager as Manager.GetType().BaseType)...

....therein lies the problem, that I'm dealing with...

....thoughts? questions? snide remarks? dirty jokes?
 
Zac,

Just curious, what is the reason that you are not using the
SoapFormatter to serialize the contents of your class? If you used that,
this would be a moot point.

Hope this helps.
 
Hi,

Thanks for posting in this group.
Why you can not cast your child class intance to your parent class?(i.e.
Employee tempObject = manager as Employee)
What is the problem about this cast?

Btw: I think your design is somewhat strange. If your base class managed
all the state of dirty variable in the array. If someone inherit a new
child class from your base class. How does your base class know this, and
add this inherited class's "dirty" value in the array?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: (e-mail address removed) (Zac)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Dynamic casting to object base class
| Date: 18 Nov 2003 08:12:48 -0800
| Organization: http://groups.google.com
| Lines: 45
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 160.79.200.162
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1069171968 5020 127.0.0.1 (18 Nov 2003
16:12:48 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Tue, 18 Nov 2003 16:12:48 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnews1.google.com!no
t-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:200192
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Alright anyone who has 2c throw it in...
|
| I am working through a custom xml serializer and have come upon a
| conundrum, given our class design.
|
| The interface implemented on the base class (base for all business
| entities) dictates that the implementing class expose a Dirty property
| (for state).
|
| The base class (that actually manages state, once for all inheritors)
| maintains an array of Dirty (bools) and each class down the chain
| knows (internally) the index in the array based on its level in the
| heirarchy.
|
| StateMgr (bool[] dirty)
| - BaseEntity
| - Person (dirty[0] = false)
| - Employee (dirty[1] = false)
| - Manager (dirty[2] = true)
|
| Given the above, each entity overrides the Dirty property from the
| base and via the override hides the indexing of the Dirty property (so
| I could call manager.Dirty = true instead of manager.Dirty[2] = true).
|
| Now in the serializer, I want to be able to serialize all of the
| exposed properties PLUS the Dirty value for each of the entities in
| the chain. In the resulting xml I would have something like <Manager
| Dirty="true false false" ... indicating that only the properties in my
| StateMgr need be updated.
|
| Getting long - but stick with me. The Dirty property is exposed
| through the interface so if I cast my Manager to an Employee the Dirty
| property through that interface is false (given the above), and in
| turn if I cast down to a Person then I also get the dirty property at
| the Person index (0) which is false.
|
| The problem is, I will only have "some object" when I serialize. I can
| use the GetType().BaseType to see the base type - but I know of no way
| to actually cast to that type (i.e. Employee tempObject = manager as
| Employee) but using dyanmic properties like (Employee tempObject =
| manager as Manager.GetType().BaseType)...
|
| ...therein lies the problem, that I'm dealing with...
|
| ...thoughts? questions? snide remarks? dirty jokes?
|
 
Back
Top