Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Why does the Visual Studio Designer Serialize Generics (e.g. List<>, SortedDictionary<>, etc.) when used as a property in a custom control
 

Why does the Visual Studio Designer Serialize Generics (e.g. List<>, SortedDictionary<>, etc.) when used as a property in a custom control

Hello,

I've experience serialization issues with Generics (e.g. a List<> exposed as a property) with issues reported in the following thread:

"Form Designer Does Not Display Form Containing Custom Control with Generics"
http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/9a9e3421-7b08-48a7-9c04-48a910711024


Instead of rehashing that thread (unless someone has a reference to a better solution), I have a very direct question:

Why does the Visual Studio Designer (WinForm) serialize a Generic List<> (LinkList<>, SortedDictionary<>, etc.)? What is the underlying reason for the serialization?
Thank in advanced,

Dave
  • Edited bydcomer Monday, August 17, 2009 4:47 PM
  •  
dcomer  Monday, August 17, 2009 4:37 PM
I'm sorry ifmy replysounded rude in any way, that was not my intention.
I cannot recall ever having custom controls that expose generic types through public properties. There are numerous issues reported to microsoft regarding generics and designer serialization. The short answer foundfor this perticular issue is:

"The issue that you have reported is actually By Design. WinForms doesn't support generics at design-time. In next release, we'll consider to improve that while resources/schedule allows."

I had to try this out my self and made a small test solution consisting of two projects, one class library and one win forms app. In the class libI defined a class "MyClass".In the win app I created a UserControl that exposed a property of the type List<ClassLibrary1.MyClass>.
I put the UserControl on my Form1 and saved. No problem. I confirmed that everything was fine by opening the Form1.resx in a text editor, copying the Base64 data serialized for my generic property and created asimple ConsoleApp thatdeserialized the data, and the object from the deserialization was what I expected, a List<ClassLibrary1.MyClass>.

So, how to trigg the issue? Well, peoplestates that thisis a versoning issue so I went back to my 2-project solution andrebuilt only the ClassLibrary1 assemblyand re-open the Form1 designer and, it crashed!
I find this very odd especially since I'm using a fixed assembly/fileversion for the ClassLibrary1 assembly. What's even more strange is that if a switch to my ConsoleApp and run it using the same Base64 data (note that the Form1.resx hasn't changed) but with the "new" ClassLibrary1.dll it works fine. So, why does the Form1 designer fail?

The designer throws the following error; "Object of type 'ClassLibrary1.MyClass[]' cannot be converted to type 'ClassLibrary1.MyClass[]'."
(When a generic list of T is serialized it will be stored to the stream as T[]. In my case this is ClassLibrary1.MyClass[]. )To meitseems that the designeris working with two different versions of the ClassLibrary1 assembly concurrently. I found both versions under "\Document and Settings\user\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies (the directory that supports the WinForms designer).
Rebuilding the whole solution doesn't help either. Restarting VS is the only way.

Just to see if this is only related to generics I changed the property type in my UserControl from List<ClassLibrary1.MyClass> to just ClassLibrary.MyClass (i.e. no generics nor collection) and tried everything all over again. Rebuilding only the ClassLibrary1.MyClass assembly messes up the Form1 designer in the exact same way as with the generic property type. However, rebuilding the whole solution makes the Form1 designer work again without restating VS.

As of now my conclusion is that WinForms designer does not support generics, and if this is due to a bugor simply a limitation in the designer is hard to tell. Even though restarting VS makes it possible to use generics this would fast become a menace to any UI developer.

/Calle
- Still confused, but on a higher level -
Calle Mellergardh  Tuesday, August 18, 2009 3:37 PM
How else should it handle it? The designer does not know the inside of whatever type you use with a generic collection. Even some non-generic types such as ArrayList are serialized. A simple type such as aninteger is stored in theFormX.Designer.cs, but how would a complex type be initialized in such a way?
So, what is your specific issue using genericsand the designer?Or let me guess, its a "load type failure" thing !? I'm afraid this happens occasionally, at leastin VS 2005 and especially if the type resides in a different assembly than your userControl. Usually rebuild and/or restart VS helps.

/Calle
- Still confused, but on a higher level -
Calle Mellergardh  Monday, August 17, 2009 9:04 PM
How else should it handle it? The designer does not know the inside of whatever type you use with a generic collection. Even some non-generic types such as ArrayList are serialized. A simple type such as aninteger is stored in theFormX.Designer.cs, but how would a complex type be initialized in such a way?
So, what is your specific issue using genericsand the designer?Or let me guess, its a "load type failure" thing !? I'm afraid this happens occasionally, at leastin VS 2005 and especially if the type resides in a different assembly than your userControl. Usually rebuild and/or restart VS helps.

/Calle
- Still confused, but on a higher level -

Calle,

I suspected that such a reply in so far as the apperent reason to seralize a user control is to maintain state. No need to guess what my issue is as my original post has a link to the issue faced my many Microsoft developers. See also the link below for Microsoft's stale input. Are you developing custom controls using generics? If so, how are you getting around this issue. I do appreciate your reply (less the sarcasim) as my purpose was to understand the process behind the designer before digging further into the issue.

With respect to "Occationally" and "rebuild/restarting", I'm not sure you have all the data.This is anknown issue that Microsoft has commented on here:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=119402

In particular, not that Microsoft reccomends using non-Generics as a temporary workaround to this issue. I'm not so sure I want to re-start the designer everytime I make a visual change to a user control during development.
dcomer  Monday, August 17, 2009 11:04 PM
I'm sorry ifmy replysounded rude in any way, that was not my intention.
I cannot recall ever having custom controls that expose generic types through public properties. There are numerous issues reported to microsoft regarding generics and designer serialization. The short answer foundfor this perticular issue is:

"The issue that you have reported is actually By Design. WinForms doesn't support generics at design-time. In next release, we'll consider to improve that while resources/schedule allows."

I had to try this out my self and made a small test solution consisting of two projects, one class library and one win forms app. In the class libI defined a class "MyClass".In the win app I created a UserControl that exposed a property of the type List<ClassLibrary1.MyClass>.
I put the UserControl on my Form1 and saved. No problem. I confirmed that everything was fine by opening the Form1.resx in a text editor, copying the Base64 data serialized for my generic property and created asimple ConsoleApp thatdeserialized the data, and the object from the deserialization was what I expected, a List<ClassLibrary1.MyClass>.

So, how to trigg the issue? Well, peoplestates that thisis a versoning issue so I went back to my 2-project solution andrebuilt only the ClassLibrary1 assemblyand re-open the Form1 designer and, it crashed!
I find this very odd especially since I'm using a fixed assembly/fileversion for the ClassLibrary1 assembly. What's even more strange is that if a switch to my ConsoleApp and run it using the same Base64 data (note that the Form1.resx hasn't changed) but with the "new" ClassLibrary1.dll it works fine. So, why does the Form1 designer fail?

The designer throws the following error; "Object of type 'ClassLibrary1.MyClass[]' cannot be converted to type 'ClassLibrary1.MyClass[]'."
(When a generic list of T is serialized it will be stored to the stream as T[]. In my case this is ClassLibrary1.MyClass[]. )To meitseems that the designeris working with two different versions of the ClassLibrary1 assembly concurrently. I found both versions under "\Document and Settings\user\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies (the directory that supports the WinForms designer).
Rebuilding the whole solution doesn't help either. Restarting VS is the only way.

Just to see if this is only related to generics I changed the property type in my UserControl from List<ClassLibrary1.MyClass> to just ClassLibrary.MyClass (i.e. no generics nor collection) and tried everything all over again. Rebuilding only the ClassLibrary1.MyClass assembly messes up the Form1 designer in the exact same way as with the generic property type. However, rebuilding the whole solution makes the Form1 designer work again without restating VS.

As of now my conclusion is that WinForms designer does not support generics, and if this is due to a bugor simply a limitation in the designer is hard to tell. Even though restarting VS makes it possible to use generics this would fast become a menace to any UI developer.

/Calle
- Still confused, but on a higher level -
Calle Mellergardh  Tuesday, August 18, 2009 3:37 PM

You can use google to search for other answers

Custom Search

More Threads

• how to create a textbox by clicking a button
• Sharing images
• order of radio buttons
• Event fired when doubleclicked on the titlebar
• How to use CodeDomSerializer and UITypeEditor correctly?
• how to set a format for date
• Managed XHTML renderer
• DesignTime licensing in a PopUp form which runs via verbs
• Displaying Child forms in a Master form
• prevent dashed border from being drawn around panel