Answered by:
Type 'XXXX.XXXXXX.XXXX.XXXXXForm' in Assembly 'XXX, Version=1.0, Culture=neutral, PublicKeyToken=134fasdfe' is not marked as serializable.

Question
-
I am using the serialization technique to clone a complex "ItemInfo" object, marked as serializable, from inside a WinForm using C# and .NET 4.0 by calling a method as follows:
public static ItemInfo CopyObject(ItemInfo iteminfo)
{
using (MemoryStream stream = new MemoryStream())
{
if (iteminfo.GetType().IsSerializable)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Context = new System.Runtime.Serialization.StreamingContext(StreamingContextStates.Clone);
formatter.Serialize(stream, iteminfo); // Exception is thrown here when executed the second time
stream.Position = 0;
return formatter.Deserialize(stream);
}
return null;
}
}
There are several controls events that trigger this method from the WinForm. It works OK the first time but throws an exception specifying that the parent form "is not marked as serializable", shown below, when called each consecutive time:
Type 'XXXX.XXXXXX.XXXX.XXXXXForm' in Assembly 'XXX, Version=1.0, Culture=neutral, PublicKeyToken='xxxxx' is not marked as serializable.
the parent form specified in the Error Message is not part of the object being serialized and this only happens if the method is call the second time or more while still the winform is open. If the form is closed and reopened it works the first time but throws Serialization Exception afterwards.
TIA
- Edited by pshirv Tuesday, February 9, 2016 9:17 PM
Tuesday, February 9, 2016 9:17 PM
Answers
-
Hello Pshirv,
Seems the issue you see is similar with the following thread, please try to mark the forms as not serialized with the NonSerializedAttrbibute to see if it can resolve the issue.
http://stackoverflow.com/questions/1495025/stuck-on-serialization-in-c-sharp
Thanks,
- Proposed as answer by Amy PengMicrosoft employee Monday, February 22, 2016 2:17 AM
- Marked as answer by Amy PengMicrosoft employee Tuesday, February 23, 2016 2:17 AM
Wednesday, February 17, 2016 3:23 AM
All replies
-
Hello Pshirv,
Regarding the error:
Type 'XXXX.XXXXXX.XXXX.XXXXXForm' in Assembly 'XXX, Version=1.0, Culture=neutral, PublicKeyToken='xxxxx' is not marked as serializable.
It has to do with what is serialized, from error message, we can see you are trying to serialize a type(XXXX.XXXXXX.XXXX.XXXXXForm) which is not marked as serializable, and it may also mean you have an event handler or some other delegates in your "ItemInfo" object.
Could you share your definition of ItemInfo so that I can take a further look at it?
At the same time, please take a look at below two threads which have the similar issues.
http://stackoverflow.com/questions/8624137/not-marked-as-serializable-error-when-serializing-a-class
http://stackoverflow.com/questions/16629270/weird-serialization-error-when-entity-is-serializable
Thanks,
Jie
Wednesday, February 10, 2016 6:52 AM -
Hello Jie,
Thank you for the response. The Form object listed in the error message is not intentionally being serialized but some how it show up in the error message. The second thing to remember is that this only happens when the method is executed the second time while the form is active. The first execution works fine. I may not be able to post the definition of the ItemInfo type due to some restrictions but I will try to mock it and its huge.
Thanks,
Pierre
Wednesday, February 10, 2016 4:24 PM -
Here is the full Error if it makes any sense which happens the second and consecutive times the method is executed when the form is stillopen:
---------------------------
Error
---------------------------
System.Runtime.Serialization.SerializationException: Type 'XXXXX.XXXXXXXXXXX.XXXApp.XXXBaseForm' in Assembly 'XXXApp, Version=9.1.14.0, Culture=neutral, PublicKeyToken=371415X1X56X7X91' is not marked as serializable.
at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)
at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
at XXXXX.XXXXXXXXXXX.Items.ItemInfo.CopyObject(ItemInfo itemInfo) in e:\XXXXX\XXXXXXXXXXX\Source\Desktop Application\Business\Items\ItemInfo.cs:line 2440
at XXXXX.XXXXXXXXXXX.XXXApp.ItemForm.EditViewItemInventory(ItemInfo itemInfo) in e:\XXXXX\XXXXXXXXXXX\Source\Desktop Application\UI\XXXApp\ItemsUI\ItemForm.cs:line 279
---------------------------
OK
---------------------------- Edited by pshirv Wednesday, February 10, 2016 11:12 PM
Wednesday, February 10, 2016 11:10 PM -
Hello pshirv
Is it possible to reproduce the issue if you just use the following code with an ItemInfo object you initialize locally (maybe in a console application)?
If it works fine, then there should be some relationship with Form info in your ItemInfo, from stack trace, we can see ItemInfo is used in ItemForm.cs which should use Form info.
public static ItemInfo CopyObject(ItemInfo iteminfo) { using (MemoryStream stream = new MemoryStream()) { if (iteminfo.GetType().IsSerializable) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Context = new System.Runtime.Serialization.StreamingContext(StreamingContextStates.Clone); formatter.Serialize(stream, iteminfo); // Exception is thrown here when executed the second time stream.Position = 0; return formatter.Deserialize(stream); } return null; } }
Thanks,
Jie
Thursday, February 11, 2016 1:25 AM -
Hello Jie,
I can call the copyObject several times one after another initially and it work fine. I did more tracing to get the relationship between the forms and this is what I found:
The first form is called ItemManagementForm is loaded
- An Item is selected and a button is clicked
- A dialog form called ChargeFeeForm with a few CheckBoxes is loaded
- A check box is checked[START]
- A new instance of ItemForm is created inside the ChargeFeeForm
-ItemForm.ItemInventory(Item) is called from ChargeFeeForm -ItemForm.ItemInventory(Item) method calles the ItemInfo.CopyObject(itemInfo) static method
-The form ItemForm is then made visible by a method from inside the ItemForm, allowing edits
-The ItemForm is then closed and control is back to ChargeFeeForm and the ChargeFeeForm is visible
[END]
- If another checkbox is selected the method ItemInfo.CopyObject(itemInfo) throws the exceptionI disabled the method that shows the ItemForm to test and it worked ok without any exceptions.
Thanks,
Friday, February 12, 2016 1:31 AM -
Hello pshirv,
According to your description, if you don't call the method that shows ItemForm, then it works fine every time, so I think the issue ( setup connection between ItemForm and ItemInfo) may be in that method which shows ItemForm,
One scenario I can think of now is there may be a field or property in your ItemInfo class, like following:
public object ParentForm;
For the first time, the value is null, so there is no issue to call CopyObject method. Then the method showing ItemForm is called, that field is assign the reference to ItemForm object.
For the second time, as the field has already a reference to ItemForm which is not serializable, so an exception saying "System.Runtime.Serialization.SerializationException: Type 'XXXForm' in Assembly 'XXXApp, Version=XXX, Culture=neutral, PublicKeyToken=XXX' is not marked as serializable." was threw.
Please double check the method showing ItemForm and also the definition of ItemInfo, and you can use "step into" debugging to check the code when it's running to get overall info.
If you can parse related info regarding method and object definition, it would be very helpful to us to do further investigation.
Thanks,
Jie
Friday, February 12, 2016 8:27 AM -
Hello Jie,
The ItemForm is displayed using the Built-in ShowDialog() method and if it is bypassed, for test purposes, the ItemForm gets created everytime but not display and the exception will not occur.
Thanks,Tuesday, February 16, 2016 1:33 AM -
Hello Pshirv,
Seems the issue you see is similar with the following thread, please try to mark the forms as not serialized with the NonSerializedAttrbibute to see if it can resolve the issue.
http://stackoverflow.com/questions/1495025/stuck-on-serialization-in-c-sharp
Thanks,
- Proposed as answer by Amy PengMicrosoft employee Monday, February 22, 2016 2:17 AM
- Marked as answer by Amy PengMicrosoft employee Tuesday, February 23, 2016 2:17 AM
Wednesday, February 17, 2016 3:23 AM