Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by gooopil · Apr 08, 2016 at 02:19 AM · serializationtypebinaryformatter

Serialiazing/Deserializing list of custom class: Could not find type

Hello,

I'm trying to send serialized data over a NetworkStream. The data is made of a Payload class, with a few fields, including one field that inherits from a custom PayloadType Class. WorldDescription is a simple class with two string fields.

I initially had this work just fine on .NET, but within Unity, I get a: ** Could not find type 'System.Collections.Generic.List`1[[ResServerLib.Data.WorldDescription, ResServerLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.*

EDIT

So I did further attemps

  • I tried to change to a List instead of the List , it works

  • I added a WorldDescription field directly to the payload (not in a List), it works

--> So it seems the issue is really about the List of WorldDescription, which at the moment is nothing more than:

 [Serializable]
     public class WorldDescription
     {
         public string worldName;
         public bool isAvailable;
 
         public WorldDescription(string worldName, bool isAvailable)
         {
             this.worldName = worldName;
             this.isAvailable = isAvailable;
         }
     }


I serialize with

          private byte[] PayloadToByteArray(Payload obj)
         {
             if (obj == null)
                 return null;
             BinaryFormatter binForm = new BinaryFormatter();
             binForm.Binder = new VersionDeserializationBinder();
 
             MemoryStream ms = new MemoryStream();
             binForm.Serialize(ms, obj);
             return ms.ToArray();
         }

And I deserialize with:

         private static Payload ByteArrayToPayload(byte[] arrBytes)
         {
             try
             {
                 MemoryStream memStream = new MemoryStream();
                 BinaryFormatter binForm = new BinaryFormatter();
                 binForm.Binder = new VersionDeserializationBinder();
 
                 memStream.Write(arrBytes, 0, arrBytes.Length);
                 memStream.Seek(0, SeekOrigin.Begin);
 
                 Payload obj = (Payload)binForm.Deserialize(memStream);
 
                 return obj;
             }
             catch (Exception ex)
             {
                 Debug.WriteLine(" *****     " + ex.Message);
                 return null;
             }
         }

Originally, I didn't use the SerializationBinder, but read that Unity had issues indicating the correct type. This effectively worked when Serializing in Unity and Deserializing in the serverapplication (edit: actually, in that case, I was only sending a PayloadType without a List<> field), but Unity still can't Deserialize server data, throwing the error above.

The SerializationBinder is (taken from here)

  public sealed class VersionDeserializationBinder : SerializationBinder
         {
             public override Type BindToType(string assemblyName, string typeName)
             {
                 if (!string.IsNullOrEmpty(assemblyName) && !string.IsNullOrEmpty(typeName))
                 {
                     Type typeToDeserialize = null;
                      assemblyName = Assembly.GetExecutingAssembly().FullName;
 
                     // The following line of code returns the type. 
                     //typeToDeserialize = Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
                     typeToDeserialize = GetType(typeName);
    
                     return typeToDeserialize;
                 }
 
                 return null;
             }

Any thoughts or suggestions?

Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by gooopil · Apr 20, 2016 at 02:16 AM

Alright, so after trying this in every way possible, I still had an issue deserializing the list<> in Unity (or serializing it for that matter). It worked on my .net app that I use as a server.

I looked into other means of serialization, and I found Protobuf-net, which incidentally has a Unity-specific release. It's pretty simple to set up, and the serialization/deserialization code is simpler and faster than BinaryFormatter. More importantly, it works!!

         private byte[] PayloadToByteArray(Payload obj)
         {
             if (obj == null)
                 return null;
 
             using (MemoryStream ms = new MemoryStream())
             {
                 Serializer.Serialize(ms, this);
                 return ms.ToArray();
             }
         }
 
         private static Payload ByteArrayToPayload(byte[] arrBytes)
         {
               using (MemoryStream memStream = new MemoryStream())
             {
                 memStream.Write(arrBytes, 0, arrBytes.Length);
                 memStream.Seek(0, SeekOrigin.Begin);
                 return Serializer.Deserialize<Payload>(memStream);
             }
         }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving Game Data to iOS Device 1 Answer

SerializationException errors!! & IOException. BinaryFormatter problems 1 Answer

Save and Load Problem, Dont quite understand. 1 Answer

"SerializationException: Unexpected binary element: 0" on Deserialization of struct 0 Answers

Android kill the save file 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges