S
Scott Simes
Hello:
I am trying to serialize only certain properties (not the methods) of my
class, say class A. I understand that I can use the [NonSerialized]
attribute on the methods and perhaps specific properties I do not wish to
serialize. It is important to know that the serialization, in my case, will
be written by the framework to the ViewState in an ASP.NET app--so it is
critical to keep the serialization content as "light" as possible (that's
why I do not want to serialize methods or non-essential properties). I
understand that I can implement the ISerializable interface, but I am trying
to understand whether there is a way to use the [Serializable] and
[Nonserialized] attributed to achieve the same result (it's less a matter of
laziness than it is wanting the ViewState 'framework' to handle the
serialization for me).
To save space in this message, please assume I have marked class A as
[Serializable] and all functions and certain properties as
[NonSerialized]...
In my code-behind module I create an object instance called objectA:
A objectA = new A();
I then set the properties of objectA:
objectA.valueOne = 1;
objectA.valueTwo = 2;
(etc.)
I then set ViewState["ObjectA"] = objectA. In the postback, I want to
deserialize ViewState["ObjectA"] into an object instance of class A.
Here are my questions:
(1) Is it possible to simply cast the return value to class A as so that it
contains all the methods and non-serialized properties of the class A
definition?
A a = (A)ViewState["ObjectA"];
(2) If (1) is not correct, what is the least amount of code I can use to
create a new object instance of class A that "inherits" all the serialized
information from ViewState["ObjectA"]?
I thought about creating another class, class B. Class B would be fully
serializable, and this is the class I would persist in the ViewState. I
could then create a version of class A that inherits from class B. The
reason I did not take this approach is that I do not know how to take an obj
ect instance of class B and force an object instance of class A to "inherit"
the values already contained in an object-instance of class B (without
overloading the "=" sign or implementing other more code-intensive
solutions).
I also thought about creating a struct, and persisting the struct through
serialization. This struct could be a public property of class A. But if I
do this, there are some downsides because the purpose of the struct would be
simply to support serialization, and serialization seems to be supported by
classes and the serialization attributes ([Serialized] and
[NonSerializable]) without the need for structs.
Any ideas on this would be greatly appreciated.
I am trying to serialize only certain properties (not the methods) of my
class, say class A. I understand that I can use the [NonSerialized]
attribute on the methods and perhaps specific properties I do not wish to
serialize. It is important to know that the serialization, in my case, will
be written by the framework to the ViewState in an ASP.NET app--so it is
critical to keep the serialization content as "light" as possible (that's
why I do not want to serialize methods or non-essential properties). I
understand that I can implement the ISerializable interface, but I am trying
to understand whether there is a way to use the [Serializable] and
[Nonserialized] attributed to achieve the same result (it's less a matter of
laziness than it is wanting the ViewState 'framework' to handle the
serialization for me).
To save space in this message, please assume I have marked class A as
[Serializable] and all functions and certain properties as
[NonSerialized]...
In my code-behind module I create an object instance called objectA:
A objectA = new A();
I then set the properties of objectA:
objectA.valueOne = 1;
objectA.valueTwo = 2;
(etc.)
I then set ViewState["ObjectA"] = objectA. In the postback, I want to
deserialize ViewState["ObjectA"] into an object instance of class A.
Here are my questions:
(1) Is it possible to simply cast the return value to class A as so that it
contains all the methods and non-serialized properties of the class A
definition?
A a = (A)ViewState["ObjectA"];
(2) If (1) is not correct, what is the least amount of code I can use to
create a new object instance of class A that "inherits" all the serialized
information from ViewState["ObjectA"]?
I thought about creating another class, class B. Class B would be fully
serializable, and this is the class I would persist in the ViewState. I
could then create a version of class A that inherits from class B. The
reason I did not take this approach is that I do not know how to take an obj
ect instance of class B and force an object instance of class A to "inherit"
the values already contained in an object-instance of class B (without
overloading the "=" sign or implementing other more code-intensive
solutions).
I also thought about creating a struct, and persisting the struct through
serialization. This struct could be a public property of class A. But if I
do this, there are some downsides because the purpose of the struct would be
simply to support serialization, and serialization seems to be supported by
classes and the serialization attributes ([Serialized] and
[NonSerializable]) without the need for structs.
Any ideas on this would be greatly appreciated.