What am I missing? ViewState object changes type from derived to base on postback.

J

John

Hi,

I derived a class from the Hashtable class, with ISerializable implemented
and the [Serializable] tag added.

Everything works fine with one exception. I store the hashtable in
ViewState, but on a postback, the object in ViewState changes to Hashtable,
instead of MyHashtable.

I'm assuming this is a simple serialization setting, but I can't seem to
find the answer.

Can somebody give me a pointer?

Thanks in advance.
John
 
J

John

Alvin Bruney said:
what does your isearlizable implementation look like? can you post a short
but complete program demonstrating the issue? Short but complete is
defined here http://www.yoda.arachsys.com/csharp/complete.html

Thanks Alvin,

Let me explain exactly what my sample program is doing.

1. I created a new class called MyHashtable, which inherits Hashtable.
2. The first time the page is loaded I instatiate a new MyHashtable object
and store it in ViewState.
3. Then I display the object type in the literal control. It is shown as
type MyHashtable.
4. I click the button
5. The object type is again shown in the literal control. But instead of
the expected MyHashtable type, it is shown as Hashtable (the base class).

Please notice in the code below, ISerializable is implemented for
MyHashtable. I believe this is necessary to accomplish my objective, but
please be aware the same thing happens even if the class is empty, like so:

public class MyHashtable : Hashtable {}

I know this is something disgustingly simple, but I can't figure out what.

Any help is appreciated. Please find code below.

Regards,
John

----------------------------
-- MyHashtable.cs
----------------------------
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace DerivedHashTable
{
[Serializable]
public class MyHashtable : Hashtable, ISerializable
{
private int irrelevantVar; // misc variable

public MyHashtable() : base() {}

public MyHashtable(SerializationInfo info, StreamingContext context)
: base( info, context)
{
irrelevantVar = info.GetInt32( "irrelevantVar");
}

public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info,context);
info.AddValue( "irrelevantVar", irrelevantVar);
}
}
}
----------------------------

----------------------------
WebForm1.aspx
----------------------------
<%@ Page language="c#" %>
<%@ Import namespace="DerivedHashTable" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
ViewState["MyHashtable"] = new MyHashtable();

lit.Text = "Object type name = " +
ViewState["MyHashtable"].GetType().Name;
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Literal ID="lit" Runat="server"/><br>
<asp:Button Text="Postback" Runat="server"/>
</form>
</body>
</html>
----------------------------
 
G

Guest

I don't know the solution (never encountered the problem), but if you're
desperate, don't extend from Hashtable; just wrap it in your object. That
way the Session can't downcast your object.

John said:
Alvin Bruney said:
what does your isearlizable implementation look like? can you post a short
but complete program demonstrating the issue? Short but complete is
defined here http://www.yoda.arachsys.com/csharp/complete.html

Thanks Alvin,

Let me explain exactly what my sample program is doing.

1. I created a new class called MyHashtable, which inherits Hashtable.
2. The first time the page is loaded I instatiate a new MyHashtable object
and store it in ViewState.
3. Then I display the object type in the literal control. It is shown as
type MyHashtable.
4. I click the button
5. The object type is again shown in the literal control. But instead of
the expected MyHashtable type, it is shown as Hashtable (the base class).

Please notice in the code below, ISerializable is implemented for
MyHashtable. I believe this is necessary to accomplish my objective, but
please be aware the same thing happens even if the class is empty, like so:

public class MyHashtable : Hashtable {}

I know this is something disgustingly simple, but I can't figure out what.

Any help is appreciated. Please find code below.

Regards,
John

----------------------------
-- MyHashtable.cs
----------------------------
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace DerivedHashTable
{
[Serializable]
public class MyHashtable : Hashtable, ISerializable
{
private int irrelevantVar; // misc variable

public MyHashtable() : base() {}

public MyHashtable(SerializationInfo info, StreamingContext context)
: base( info, context)
{
irrelevantVar = info.GetInt32( "irrelevantVar");
}

public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info,context);
info.AddValue( "irrelevantVar", irrelevantVar);
}
}
}
----------------------------

----------------------------
WebForm1.aspx
----------------------------
<%@ Page language="c#" %>
<%@ Import namespace="DerivedHashTable" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
ViewState["MyHashtable"] = new MyHashtable();

lit.Text = "Object type name = " +
ViewState["MyHashtable"].GetType().Name;
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Literal ID="lit" Runat="server"/><br>
<asp:Button Text="Postback" Runat="server"/>
</form>
</body>
</html>
 
J

John

William Sullivan said:
I don't know the solution (never encountered the problem), but if you're
desperate, don't extend from Hashtable; just wrap it in your object. That
way the Session can't downcast your object.

Thanks William,

That's what I eventually wound up doing.

But I must say, I would like to know what was happening there.

If anybody knows, I'd love to have a clue thrown my way. ;-)

Regards,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top