繁体   English   中英

写入字节数组时如何读取列表

[英]How to read a list when writing to a Byte Array

我正在使用C#,Google Play游戏服务和Unity。 我需要能够从包含浮点数的列表中写入字节数组,然后以另一种方法再次读取它们。 我可以做到这一点,以便它当然将浮点数存储在列表中(我也可以在输出中看到它),但是当我尝试写入字节数组时打印列表的Count时,它告诉我列表Count是零...这很奇怪,因为当我检查此方法之外的其他任何地方的列表时,它会为我提供正确的尺寸。 我究竟做错了什么?

我已经将其他两个类的代码部分添加到了列表中。 再次,我已经知道它已成功添加到列表中,当我在ToByte方法中使用im时,它只是不会给我更正计数。

匹配数据类

private List<float> mRuneTypes = new List<float>();

 public byte[] ToBytes() { //here is where I am trying to write to the mem stream

    MemoryStream memStream = new MemoryStream();
    BinaryWriter w = new BinaryWriter(memStream);

    w.Write(Header); //writes header ID to memory

    w.Write(mRuneTypes.Count); //this shows up as zero but everywhere else as 4

    for (int i =0; i<4; i++) {


        w.Write(mRuneTypes[i]); //i get an index out of bounds since list is empty?


        Debug.Log( "Writing the color val is: " + mRuneTypes[i]);


    }

    networkStatus = "has wrote to mem"; //just something for me to check

    w.Close();

    byte[] buf = memStream.GetBuffer();

    memStream.Close();

    return buf;
}


public void AddRuneTypes(float runeIntDesignation) { //this is where the values are passed and added to the list

    mRuneTypes.Add(runeIntDesignation);

    Debug.Log ("the before tobyte count is : " + mRuneTypes.Count); //gives the correct count of 4... why isnt it the same in ToByte[]??

}

/////来自TurnedBasedGameOp类

public void passRunes(float runeIntDesignation) {
    //sent runes are passed from the block selector to this class to matchdatarunes

    mMatchData.AddRuneTypes (runeIntDesignation);
    print ("rune type of " + runeIntDesignation + " has been added");
}

/////来自块选择类

this.GetComponent<TurnedBasedGameOrganizer>().passRunes(selectedBlocks[i].gameObject.GetComponent<MeteorCode>().runeIntDesignation);//this sends the runes selected to the TurnedBasedGameOp Class above this code is contained in a larger For loop which doesnt need to be included

在此代码段中, mRuneTypes列表为空,我认为您需要粘贴填充列表数据的代码,以便我们尝试找出不起作用的原因。

对于for循环语句,请不要使用幻数“ 4”,请使用以下代码代替您的代码,因此索引不足异常将消失。

for(int i=0; i<mRuneTypes.count; i++){
     // Do your logic code.
 }

希望这会有所帮助。 谢谢。

这是不好的...

for (int i =0; i < 4; i++) 

不好的原因是因为从一开始,您就假定mRuneTypes包含项目的集合,但是从我看到的代码中,您所做的全部都是初始化一个集合,您尚未填充它。

如果您要遍历列表中的每个项目,请使用foreach语句或使用for语句,它们只会增加到集合中的项目总数:

int i = 0; i < mRuneTypes.Count; i++`.

可能会帮助您的另一个技巧是在调试时使用的这种小辅助方法...

public static class Extension
{
    public static T Dump<T>(this T tObject)
    {
        tObject.GetType()
               .GetProperties()
               .Where(property => property.GetValue(tObject) != null)
               .Select(property => string.Format("{0}: {1}", property.Name, property.GetValue(tObject)))
               .ToList()
               .ForEach(Console.WriteLine);

        return tObject;
    }
}

在任何对象的末尾,只需添加.Dump()并观察输出以查看对象的属性...

我想出了答案。 这与我用来将数据发送到比赛数据符文类的turnbasedgameorganizer类有关。 我刚结束合并它们。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM