xml 序列化程序导致 photon rpc 命令中的空引用

xml serializer causes nullreference in photon rpc comand

我得到了这段代码 xml 序列化程序并试图让它工作,但创建的对象包含 nullreference

public  byte[] SerializeObject <T>(T serializableObject)
{
    T obj = serializableObject;

    using (MemoryStream stream = new MemoryStream())
    {
        System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));

        x.Serialize (stream,obj);

        return stream.ToArray ();
    }
}

public  T DeserializeObject <T>(byte[] serilizedBytes)
{
    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer (typeof(T));

    using (MemoryStream stream = new MemoryStream(serilizedBytes)) 
    {
        return (T) x.Deserialize (stream);
    } 
}

我使用此函数初始化 class 个变量,但找不到 null

public void updateinfo(int []card, int lmin, int lbom, int pass, int mxint,int cardinturn, int lastsim, int fplayer, float ang, int playid,string namepl,int numberofcard){
    o1=new playerupdates();
    o1.cardstoad = new int[card.Length];
    for(int i=0;i<card.Length;i++){
        o1.cardstoad[i]=card[i];
    }
o1.lastmin=lmin;
o1.lastbomb=lbom;
o1.passnumber=pass;
o1.maxvalueinturn=mxint;
o1.cardsinturn=cardinturn;
o1.lastsimiliarvalue=lastsim;
o1.firstplayer=fplayer;
o1.angle=ang;
o1.playerid=playid; 
o1.playername=namepl;
o1.cardsin=numberofcard;

}

我搜索了一个解决方案,但没有找到。初始化 class 变量的函数是否正确? 编辑 class代码

public int []cardstoad { get; set; }
public int lastmin { get; set; }
public int lastbomb { get; set; }
public int passnumber{ get; set; } 
public int maxvalueinturn { get; set; }
public int cardsinturn { get; set; }
public int lastsimiliarvalue { get; set; }
public int firstplayer { get; set; }
public float angle{ get; set; }
public int playerid { get; set; }
public string playername { get; set; }
public int cardsin{ get; set; }

你的问题是 o1 没有做任何事情。 updateinfo 正在设置值,但 o1 在从 updateinfo 返回后丢失。请参阅下面的代码进行更正

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] card = {1,2,3,4,5};
            int lmin = 4;
            int lbom = 5;
            int pass = 6;
            int mxint = 7;
            int cardinturn = 8;
            int lastsim = 9;
            int fplayer = 10;
            float ang = 30.0F;
            int playid = 100;
            string namepl = "abc";
            int numberofcard = 26;
            playerupdates pU = new playerupdates();
            pU.updateinfo(card, lmin, lbom, pass, mxint, cardinturn, lastsim, fplayer, ang, playid, namepl, numberofcard);
            byte[] data = pU.SerializeObject<playerupdates>(pU);
            string test = Encoding.UTF8.GetString(data);    //test to see if serialize is working correctly
            playerupdates newPU = pU.DeserializeObject<playerupdates>(data);
        }
    }

    [XmlRoot("playerupdates")]
    public class playerupdates
    {
        [XmlElement("cardstoad")]
        public int[] cardstoad { get; set; }
        [XmlElement("lastmin")]
        public int lastmin { get; set; }
        [XmlElement("lastbomb")]
        public int lastbomb { get; set; }
        [XmlElement("passnumber")]
        public int passnumber { get; set; }
        [XmlElement("maxvalueinturn")]
        public int maxvalueinturn { get; set; }
        [XmlElement("cardsinturn")]
        public int cardsinturn { get; set; }
        [XmlElement("lastsimiliarvalue")]
        public int lastsimiliarvalue { get; set; }
        [XmlElement("firstplayer")]
        public int firstplayer { get; set; }
        [XmlElement("angle")]
        public float angle { get; set; }
        [XmlElement("playerid")]
        public int playerid { get; set; }
        [XmlElement("playername")]
        public string playername { get; set; }
        [XmlElement("cardsin")]
        public int cardsin { get; set; }

        public void  updateinfo(int[] card, int lmin, int lbom, int pass, int mxint, int cardinturn, int lastsim, int fplayer, float ang, int playid, string namepl, int numberofcard)
        {
            //o1 not needed
            //playerupdates o1 = new playerupdates();
            cardstoad = new int[card.Length];
            for (int i = 0; i < card.Length; i++)
            {
                cardstoad[i] = card[i];
            }
            lastmin = lmin;
            lastbomb = lbom;
            passnumber = pass;
            maxvalueinturn = mxint;
            cardsinturn = cardinturn;
            lastsimiliarvalue = lastsim;
            firstplayer = fplayer;
            angle = ang;
            playerid = playid;
            playername = namepl;
            cardsin = numberofcard;
        }

        public byte[] SerializeObject<T>(T serializableObject)
        {
            T obj = serializableObject;

            using (MemoryStream stream = new MemoryStream())
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));

                x.Serialize(stream, obj);

                return stream.ToArray();
            }
        }

        public T DeserializeObject<T>(byte[] serilizedBytes)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));

            using (MemoryStream stream = new MemoryStream(serilizedBytes))
            {
                return (T)x.Deserialize(stream);
            }
        }

    }
}
​