Showing posts with label members. Show all posts
Showing posts with label members. Show all posts

Monday, March 26, 2012

Serialization

Hi,

I have to serialize a class which consists of numerous members in it. I know that it can be serialized with the [Serializable] attribute. But my problem is that I wanna serialize only few members. Is there any way that allows us to serialize only few members in a class?

Please help.

Decorate the fields you want to exclude with [NonSerialized] .


You can include the [NonSerialized] attribute for the members which you do not wish to be serialized as shown below.

[Serializable]
class ABC
{
int x;
int y;
[NonSerialized]int z;//z will not be serializable.
}

I hopw this helped you.

SrikanthReddy.L

Saturday, March 24, 2012

Serialize DateTime

Hi -

I'm currently using a BinaryFormater to serialize my ObjectGraph. Private members of type string serialize and deserialise well however private members of type DateTime do not deserialize back into their original datetime values - something is lost.

Can someone direct me on solving this problem? For the moment I'm casting my DateTime values to string before serialization and then using DateTime.Parse(...) to get the string representation back to type DateTime. It works but I'd prefer the correct way I must be doing something wrong!

The following fragment (C#) is how i'm marking my class for serialization:

[Serializable()]
public class SessionData
{
private DateTime _sessionDate;

Thanks for any direction on this.How about serialising the Ticks property of your DateTime object? Then you can instantiate a new DateTime object from that single Ticks value ...

(I haven't tested this ... just thinking aloud.)
Thanks for the suggestion. That would probably work as it would convert the DateTime to a Long data type, but I'm wondering why the problem with DateTime ?

I'm sure the framework is designed to serialise DateTime without the need for casting (pretty stupid if this is not the case!) so what am I missing ?
Well, DateTime is a Struct ... so it's closer to a class than it is to a base type (such as a string or integer). Hence, I don't think serialising will be quite so automatic ... to serialise a class, you generally have to do a bit of work.

But, as to that "bit of work", I was just wondering aloud whether Ticks might make your job easier.

And I could be completely wrong ... 'tis just a suggestion :)

Serializing Collections with Interface members

I have a collection class and in order to make it type safe etc , I only add
and retrive members of Interface types.
But during XMLSerialization I get errors.
Is there any workaround ?
public class OrgRatingCollection:CollectionBase
{
public void Add(IRating item)
{
this.List.Add(item);
}
public IRating this[int index]
{
get
{
return (IRating) this.List[index];
}
set
{
this.List[index] = value;
}
}
}
}to serialize a collection, all object in the collection must be
serializable. defining a interface has no impact.
-- bruce (sqlwork.com)
"Akshay Kumar" <akshay349@.hotmail.com> wrote in message
news:OkVn0Q6wFHA.3720@.TK2MSFTNGP11.phx.gbl...
>I have a collection class and in order to make it type safe etc , I only
>add
> and retrive members of Interface types.
> But during XMLSerialization I get errors.
> Is there any workaround ?
>
> public class OrgRatingCollection:CollectionBase
> {
> public void Add(IRating item)
> {
> this.List.Add(item);
> }
>
> public IRating this[int index]
> {
> get
> {
>
> return (IRating) this.List[index];
> }
> set
> {
>
> this.List[index] = value;
> }
> }
> }
> }
>

Serializing Collections with Interface members

I have a collection class and in order to make it type safe etc , I only add
and retrive members of Interface types.

But during XMLSerialization I get errors.

Is there any workaround ?

public class OrgRatingCollection:CollectionBase

{

public void Add(IRating item)

{

this.List.Add(item);

}

public IRating this[int index]

{

get

{

return (IRating) this.List[index];

}

set

{

this.List[index] = value;

}

}

}

}to serialize a collection, all object in the collection must be
serializable. defining a interface has no impact.

-- bruce (sqlwork.com)

"Akshay Kumar" <akshay349@.hotmail.com> wrote in message
news:OkVn0Q6wFHA.3720@.TK2MSFTNGP11.phx.gbl...
>I have a collection class and in order to make it type safe etc , I only
>add
> and retrive members of Interface types.
> But during XMLSerialization I get errors.
> Is there any workaround ?
>
> public class OrgRatingCollection:CollectionBase
> {
> public void Add(IRating item)
> {
> this.List.Add(item);
> }
>
> public IRating this[int index]
> {
> get
> {
>
> return (IRating) this.List[index];
> }
> set
> {
>
> this.List[index] = value;
> }
> }
> }
> }