Denis Aleshin
Denis Aleshin

Reputation: 3

C# Cannot serialize object because MainViewModel is not marked Serializable

i just started to play a little bit with Binary Serialization.

I have a class "SerializeMe" which i want to serialize:

[Serializable]
public class ViewModelBase : INotifyPropertyChanged
{
    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void RaisePropertyChanged(
                      [CallerMemberName]string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

[Serializable]
public class SerializeBase : ViewModelBase
{
  .
  .
  .
}

Class i want to serialize:

[Serializable]
public class SerializeMe : SerializeBase 
{
     .
     .
     .
}

In my MainViewModel i have an ObservableCollection of type "SerializeBase" and a Method which serializes the first item in the collection:

public class MainViewModel : ViewModelBase
{
   private ObservableCollection<SerializeBase> _workspaces;

   public MainViewModel()
   {
      _workspaces = new ObservableCollection<SerializeBase>
   // EDIT
                    {
                         new SerializeMe(),
                         new SerializeMe()
                    }
   // EDIT END
   }

   public ObservableCollection<SerializeBase> Workspaces
   {
         get { return _workspaces; }
         set
         {
               if (value == _workspaces)
                   return;

               _workspaces = value;
               RaisePropertyChanged();
         }
   }

   public void SerializeFirst()
   {
        var fisrtItem = _workspaces.FirstOrDefault();
        if (firstItem == null)
            return;

        using(var stream = new FileStream("file.bin", FileMode.Create))
        {
            new BinaryFormatter().Serialize(stream, firstItem);
        }
   }
}

All this works just if i mark my MainViewModel as Serializable.

This works:

namespace Namespace
{
   [Serializable]
   public class MainViewModel : ViewModelBase
   .
   .
   .

This not:

namespace Namespace
{
   public class MainViewModel : ViewModelBase
   .
   .
   .

Error detail: The type MainViewModel... is not marked as Serializable.

Could someone explain why my MainViewModel has to be serializable? I don't get it.

Thanks.

Upvotes: 0

Views: 975

Answers (2)

user1228
user1228

Reputation:

It's because what you want to serialize has a reference to the MainViewModel. How, I don't know. But I do know it isn't magic.

if you serialize an instance of X, and it fails because type Y isn't serializable, then that instance of X holds a reference to an instance of type Y. That's 100% sure.

Normally, you should debug, and right before serializing you check that object graph from top to bottom. For binary serialization, this includes events, as binary serialization will serialize event listeners as well.

Binary serialization is so 1.0. You should look into more recent developments in serialization technologies. The NetDataContractSerializer and xaml serialization are good fits for most of your .NET needs. Json is fine for when your Types aren't that important. None of these will serialize event listeners.

Upvotes: 0

sidjames
sidjames

Reputation: 117

I've had this exact problem recently while trying to serialize a large data set.

The problem is that somehow or other one of your models has ended up with a link to some element from the view model\commands\converters you are using, and as such the serializer thinks it needs to serialize the entire view model.

If you do mark the main view model as Serializable it will start going through all other view models and commands connected to your main view model and give you the same problem with those.

You need to find out what is getting connected and where and then stop it from happening.

Upvotes: 1

Related Questions