简体   繁体   English

我单位客户端的序列化object结果和c#控制台服务器的结果不一样

[英]The serialized object result of my unit client is different from that of the c# console server

same class for serialize:相同的 class 用于序列化:

using System;
[Serializable]
class Class2
{
    public int a;
}

same serialize function:同样序列化 function:

        public static byte[] Serialize(object obj)
        {
            if (obj == null || !obj.GetType().IsSerializable)
            {
                return null;
            }

            BinaryFormatter formatter = new BinaryFormatter();
            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, obj);
                byte[] data = stream.ToArray();
                return data;
            }
        }

sever:断绝:

Class2 c = new Class2();
c.a = 1 ;
byte[] data = NetworkUtils.Serialize(c);
Console.WriteLine( BitConverter.ToString(data));

unity client:统一客户端:

Class2 c = new Class2();
c.a = 1;
byte[] data = NetworkUtils.Serialize(c);
Debug.Log("测试序列化 " + BitConverter.ToString(data));

is this problem about .net edition?这个问题是关于 .net 版本的吗? unity is standard2,server is core3.1 unity是standard2,server是core3.1

unity result: 00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-46-41-73-73-65-6D-62-6C-79-2D-43-53-68-61-72-70-2C-20-56-65-72-73-69-6F-6E-3D-30-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-06-43-6C-61-73-73-32-01-00-00-00-01-61-00-08-02-00-00-00-01-00-00-00-0B UnityEngine.Debug:Log (object)统一结果:00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-46-41 -73-73-65-6D-62-6C-79-2D-43-53-68-61-72-70-2C-20-56-65-72-73-69-6F-6E-3D-30 -2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20 -50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-06 -43-6C-61-73-73-32-01-00-00-00-01-61-00-08-02-00-00-00-01-00-00-00-0B UnityEngine.Debug:日志(对象)

server result: 00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-3B-47-61-6D-65-2C-20-56-65-72-73-69-6F-6E-3D-31-2E-30-2E-30-2E-30-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-06-43-6C-61-73-73-32-01-00-00-00-01-61-00-08-02-00-00-00-01-00-00-00-0B服务器结果:00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-3B-47 -61-6D-65-2C-20-56-65-72-73-69-6F-6E-3D-31-2E-30-2E-30-2E-30-2C-20-43-75-6C -74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F -6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-06-43-6C-61-73-73-32-01-00-00-00-01 -61-00-08-02-00-00-00-01-00-00-00-0B

PS:If I make the class into a DLL file and import it, this problem will not appear,But this method is too troublesome PS:如果我把class做成DLL文件再导入就不会出现这个问题了,但是这个方法太麻烦了

If you compare the strings what you get is on your server如果你比较你得到的字符串在你的服务器上

FAssembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Class2 a

And in Unity在 Unity 中

Game, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Class2 a

(I left out the not printable Chars) The difference is the Assembly name. (我省略了不可打印的字符)不同之处在于程序集名称。 In Unity it is Game , on your server it is FAssembly .在 Unity 中是Game ,在您的服务器上是FAssembly


In general as said STOP using BinaryFormatter at all!一般来说, 完全停止使用 BinaryFormatter! !

Despite the security concerns it is also a huge waste of bandwidth!尽管存在安全问题,但它也是对带宽的巨大浪费!

In Unity you send 111 and on the server even 122 bytes!在 Unity 中发送111 ,在服务器上发送122字节!

Your class has a single int field which only requires 4 bytes of data!您的 class 有一个仅需要4个字节数据的int字段!

As said I would rather implement如前所述,我宁愿实施

[Serializable]
class Class2
{
    public int a;

    public byte[] ToBytes()
    {
        return BitConverter.GetBytes(a);
    }

    public Class2(){}

    public Class2(byte[] bytes)
    {
        a = BitConverter.ToInt32(bytes);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Node.js 服务器环境中处理来自 C# 客户端的序列化 object 的最佳方法 - The best way to handle serialized object from C# client in Node.js server env 将Protobuf序列化对象从C#发送到Java服务器不起作用? - Sending protobuf serialized object from c# to java server not working? 在C#中从服务器发送的客户端控制台上显示字符串数组 - Displaying a string array on client console sent from server in c# 是什么导致在单元测试(NUnit或MSTest)中从C#调用的C ++函数产生的结果不同于在控制台应用程序中运行的同一代码? - What is causing a C++ function called from C# in a unit test (NUnit or MSTest) to produce a different result from the same code run in a console app? 从控制台应用程序C#访问不同线程中的对象 - Get access to object in the different thread from console app c# 实例化对象不同于控制台的输出。 (C#UNITY) - Instantiated Object is different from the output of the console. (C# UNITY) C#控制台自动将服务器重新连接到客户端 - C# Console Auto Reconnect Server to Client C#序列化对象版本 - C# serialized object versions 在SQL Server和C#中运行查询的结果不同 - Different result running a query in SQL Server and from C# 从C#中的序列化字符串中提取JSON对象 - Extracting a JSON object from a serialized string in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM