通常Type.GetType 是未编译进 dll中的类 若调用 dll 类型 需要新的方法
将协议 头部 加上 代表类型的 字符串
在解析时候 ,先获取 代表 类型的 字符串,
通过字符串 获取类型 进行 反序列化 得到 一个mess 就可以强制转化对应类型对象数据
以下通过字符串参数 获取dll内的类型的方法
public static Type typen(string typeName)
{
Type type = null;
Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
int assemblyArrayLength = assemblyArray.Length;
for (int i = 0; i < assemblyArrayLength; ++i)
{
type = assemblyArray[i].GetType(typeName);
if (type != null)
{
return type;
}
}
for (int i = 0; (i < assemblyArrayLength); ++i)
{
Type[] typeArray = assemblyArray[i].GetTypes();
int typeArrayLength = typeArray.Length;
for (int j = 0; j < typeArrayLength; ++j)
{
if (typeArray[j].Name.Equals(typeName))
{
return typeArray[j];
}
}
}
return type;
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using ProtoBuf;
using UnityEngine;
using UnityEngine.SceneManagement;
using MyProto;
using System.Reflection;
/***************************************************************
* Copyright 2021 By 盛世在线
* Author: YangKingFire
* Create: 2021/12/11
* Note :
***************************************************************/
public class mapPosXImage : MonoBehaviour
{ static float timeN = 3f;
float timer = timeN;
public Animator animator;
public Rigidbody rigidbody;
private Vector3 pos;
//--背包数据
public List<ModeData> modeDatasBag;
AnimatorStateInfo stateInfo;
ProtobufSerializer serializer = new ProtobufSerializer();
void Start()
{
MyProto.MsgLogin req = new MyProto.MsgLogin();
MyProto.MsgLogin playerInfo = new MyProto.MsgLogin();
playerInfo.account = "哈哈";
playerInfo.password = "123456";
//此 二进制串用于 与 服务器交互
byte[] data = SerializeProto(playerInfo);
MyProto.MsgLogin proto3 = new MyProto.MsgLogin();
Type type = Type.GetType("MyProto.MsgLogin");
//用于 读取 服务器发过来的数据
ProtoBuf.IExtensible proto1 = DeSerializeProto(data, Type.GetType("MyProto.MsgLogin"));//proto3.GetType())as MyProto.MsgLogin;
MyProto.MsgLogin proto = (MyProto.MsgLogin)proto1;
Debug.Log(typen("MyProto.MsgLogin").Name+ proto.account);
}
//序列化
byte[] SerializeProto(ProtoBuf.IExtensible proto)
{
byte[] data = null;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
serializer.Serialize(ms, proto);
if (ms.Length > 0)
{
data = new byte[(int)ms.Length];
ms.Seek(0, System.IO.SeekOrigin.Begin);
ms.Read(data, 0, data.Length);
}
ms.Close();
}
return data;
}
//反序列化
ProtoBuf.IExtensible DeSerializeProto(byte[] data, Type type)
{
ProtoBuf.IExtensible proto = null;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.Write(data, 0, data.Length);
ms.Seek(0, System.IO.SeekOrigin.Begin);
proto = serializer.Deserialize(ms, null, type) as ProtoBuf.IExtensible;
ms.Close();
}
return proto;
}
public static Type typen(string typeName)
{
Type type = null;
Assembly[] assemblyArray = AppDomain.CurrentDomain.GetAssemblies();
int assemblyArrayLength = assemblyArray.Length;
for (int i = 0; i < assemblyArrayLength; ++i)
{
type = assemblyArray[i].GetType(typeName);
if (type != null)
{
return type;
}
}
for (int i = 0; (i < assemblyArrayLength); ++i)
{
Type[] typeArray = assemblyArray[i].GetTypes();
int typeArrayLength = typeArray.Length;
for (int j = 0; j < typeArrayLength; ++j)
{
if (typeArray[j].Name.Equals(typeName))
{
return typeArray[j];
}
}
}
return type;
}
}