Type in assembly is not marked as serializable
Asked Answered
S

3

7

I have an entityClass that I wish to serialize as a clone. But this class has a reference assembly from a custom framework which I don't have the access to the code. Whenever I try to serialize entityClass object, it throw the exception,

Type ... in Assembly '..., Version=4.1.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Stay answered 9/7, 2015 at 3:0 Comment(0)
S
11

Assuming that the problem is that a field/property on your object is of the problem type, you need to either mark the field/property as NonSerialized or create a derivative of the type which is marked as Serializable

If you derive from the type and mark it as Serialized, you will most likely have to create the serialization logic yourself. This requires you to implement ISerializable and a serialization constructor which takes SerializationInfo and StreamingContext.

This link may help.

Salonika answered 9/7, 2015 at 3:21 Comment(0)
T
8

If you have PropertyChanged event then set it to [NonSerialized] in all objects(classes) you will Serialize.

    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
Tridactyl answered 19/4, 2017 at 20:29 Comment(1)
This fixed a similar problem for me. All events in C# must be [field: NonSerialized] or they try to 'save' them as a function.Fly
T
0

If you get this error, try adding Serializable attribute to your class. Example:

   [Serializable]
   public class MyModel 
   {
      public int? ID { get; set; }
      ...
   }
Ticonderoga answered 14/10, 2021 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.