Syntax for Serializing generic ref class

E

Edward Diener

Following the example in the help for the generic List class, which
shows the Serializable attribute being used on the generic class, like so:

[SerializableAttribute]
generic<typename T>
public ref class List : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable

I try to do the same for my own generic class, like so:

[System::SerializableAttribute]
generic <typename T>
public ref class AClass
{
// Lots of goodies
};


only to be met with compiler error:

error C2059: syntax error : 'generic'

on line 2. If I remove the [System::SerializableAttribute] , all is
well, but naturally I want it so that my class can be serialized.

What is the magic incantation <g> to get this to work properly ? I have
tried a number of different variations without getting this to work, so
evidently my genii is on vacation.
 
C

Chris Taylor

Hi,

Put the attribute after the generic declaration ie.

generic<typename T>
[SerializableAttribute]
public ref class List : IList<T>, ICollection<T> ...

Hope this helps
 
B

Ben Voigt

Chris Taylor said:
Hi,

Put the attribute after the generic declaration ie.

generic<typename T>
[SerializableAttribute]
public ref class List : IList<T>, ICollection<T> ...

Hope this helps

This is clearly a documentation bug at
http://msdn2.microsoft.com/en-us/library/skef48fy.aspx
--
Chris Taylor
http://dotnetjunkies.com/weblog/chris.taylor


Edward Diener said:
Following the example in the help for the generic List class, which shows
the Serializable attribute being used on the generic class, like so:

[SerializableAttribute]
generic<typename T>
public ref class List : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable

I try to do the same for my own generic class, like so:

[System::SerializableAttribute]
generic <typename T>
public ref class AClass
{
// Lots of goodies
};


only to be met with compiler error:

error C2059: syntax error : 'generic'

on line 2. If I remove the [System::SerializableAttribute] , all is well,
but naturally I want it so that my class can be serialized.

What is the magic incantation <g> to get this to work properly ? I have
tried a number of different variations without getting this to work, so
evidently my genii is on vacation.
 

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

Top