471,071 Members | 1,331 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Dynamic casting to object base class

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?
Nov 15 '05 #1
2 2429
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zac" <zn*****@hotmail.com> wrote in message
news:7e************************@posting.google.com ...
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?

Nov 15 '05 #2

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: zn*****@hotmail.com (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: <7e************************@posting.google.com>
| 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: gr**********@google.com
| NNTP-Posting-Date: Tue, 18 Nov 2003 16:12:48 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed 00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!postnew s1.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?
|

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Radde | last post: by
9 posts views Thread by maynard | last post: by
4 posts views Thread by Zark3 | last post: by
2 posts views Thread by Mike Stevenson | last post: by
13 posts views Thread by DaTurk | last post: by
6 posts views Thread by Nick Keighley | last post: by
9 posts views Thread by Jess | last post: by
7 posts views Thread by Christopher Pisz | last post: by
9 posts views Thread by Taras_96 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.