Groups | Blog | Home
all groups > dotnet windows forms designtime > april 2005 >

dotnet windows forms designtime : Code serialization for a collection property


Bernd S
4/5/2005 1:12:31 PM
Hi,

how is it possible to serialize a collection into code similar as the
ControlCollection serialization? The problem what I see is that
CollectionCodeComSerializer is an internal class which cannot be overrided.

Thanks,
Bernd


Bernd S
4/6/2005 10:16:44 AM
Yes, but as far as I know I additionally have to use a
DesignerSerializerAttribute for the collection class to specify an
appropriate CodeDomSerializer.
"System.ComponentModel.Design.Serialization.CollectionCodeDomSerializer"
would be appropriate, but it is an internal class. So I wonder whether I
have to write my own CollectionCodeDomSerializer.

"joeycalisay" <hcalisay@_spamkiller_codex-systems.com> schrieb im
Newsbeitrag news:uJ8qIylOFHA.1172@TK2MSFTNGP12.phx.gbl...
[quoted text, click to view]

joeycalisay
4/6/2005 11:55:53 AM
Have you tried adding DesignerSerializationVisibility.Content attribute to
the Collection property?

--
Joey Calisay
http://spaces.msn.com/members/joeycalisay/


[quoted text, click to view]

joeycalisay
4/7/2005 12:00:00 AM
CollectioCodeDomSerializer is used by default of all Collection type
properties by VS. In a normal scenario, you don't have to extend it, just
have a Content attribute on the collection property and it will be
serialized, sometimes you might need a TypeConverter, I myself in my
experience did not need to extend the said class. Do you really want to
extend it and why?

--
Joey Calisay
http://spaces.msn.com/members/joeycalisay/


[quoted text, click to view]

Bernd S
4/7/2005 11:00:30 AM
Ok, I do not need to override the class CollectionCodeDomSerializer. There
was another problem that
[DesignerSerializer( typeof( CollectionCodeDomSerializer), typeof(
CodeDomSerializer ) )]

is not possible because CollectionCodeDomSerializer is an internal class but
now I use

[DesignerSerializer(
"System.ComponentModel.Design.Serialization.CollectionCodeDomSerializer",
typeof( CodeDomSerializer ) )]

and so the CollectionCodeDomSerializer is used for my collection.

Now code serialization works fine, i.e. the CollectionCodeDomSerializer
generates a line like

this.MyCollectionProp.Add(this.MyItemComponent);

but the deserialization seems not to work after closing and then re-opening
the document: Only an empty collection is created, i.e. the collection
editor shows no elements.
Do you have a clue what the reason could be?



"joeycalisay" <hcalisay@_spamkiller_codex-systems.com> schrieb im
Newsbeitrag news:%231X2KkwOFHA.3408@TK2MSFTNGP14.phx.gbl...
[quoted text, click to view]

joeycalisay
4/7/2005 5:35:35 PM
Actually the DesignerSerializer is not needed, VS defaults to use it for
collection types. Can you paste code for your custom collection class
MyCollectionProp

--
Joey Calisay
http://spaces.msn.com/members/joeycalisay/


[quoted text, click to view]

Bernd S
4/8/2005 12:00:00 AM
The collection property is implemented as

[Category( SmfAttrTypeText.ISmfPageControl.AllVisConfigPageSpecs.Category )]
[Description(
SmfAttrTypeText.ISmfPageControl.AllVisConfigPageSpecs.Description )]
[DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
public PageSpecCollection AllVisConfigurablePageSpecs
{
get
{
return this.Runtime.AllVisConfigurablePageSpecs;
}

// leaving out this set accessor causes the code deserialization to
work!!!
set
{
this.Runtime.AllVisConfigurablePageSpecs = value;
}
}

and the custom collection class as

public class PageSpecCollection : CollectionBase
{
public PageSpecCollection()
{
}
public PageSpecCollection ( PageSpecCollection value)
{
this.AddRange(value);
}
public PageSpecCollection( PageSpec[] value)
{
this.AddRange(value);
}
public int Add(PageSpec value)
{
return this.List.Add(value);
}
public void AddRange(PageSpec[] value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
for (int index = 0; index < value.Length; index++)
{
this.Add(value[index]);
}
}
public void AddRange(PageSpecCollection value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
int cnt = value.Count;
for (int index = 0; index < cnt; index++)
{
this.Add(value[index]);
}
}
public bool Contains(PageSpec value)
{
return this.List.Contains(value);
}
public void CopyTo(PageSpec[] array, int index)
{
this.List.CopyTo(array, index);
}
public int IndexOf(PageSpec value)
{
return this.List.IndexOf(value);
}
public void Insert(int index, PageSpec value)
{
this.List.Insert(index, value);
}
public void Remove(PageSpec value)
{
this.List.Remove(value);
}
public PageSpec this[int index]
{
get
{
return (PageSpec) this.List[index];
}
set
{
this.List[index] = value;
}
}


The strange thing is that meanwhile I found out that leaving out the set
accessor of the collection property causes the code deserialization to work.
But I cannot imagine why.


"joeycalisay" <hcalisay@_spamkiller_codex-systems.com> schrieb im
Newsbeitrag news:%23qvyoU1OFHA.2520@tk2msftngp13.phx.gbl...
[quoted text, click to view]

Mick Doherty
4/8/2005 10:54:43 AM
A Collection property is ReadOnly because you want to modify it's items and
not exchange the collection object itself.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


[quoted text, click to view]
AddThis Social Bookmark Button