将类转换为流 [英] Convert Class To Stream

查看:98
本文介绍了将类转换为流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件中保存gamesdata但是在给出错误时无法将GameData转换为System.To.Stream



严重性代码描述项目文件行

错误CS1503参数1:无法从GameData转换为System.IO.StreamAssembly-CSharp-vs GameControle.cs 43





.........帮助我........





< pre lang =c#> 使用 UnityEngine;
使用 System.Collections;
使用系统;
使用 System.IO;
使用 System.Runtime.Serialization.Formatters.Binary;


public class GameControler:MonoBehaviour {

public static GameControler实例;

私人 GameData数据;

public int cruntSchore;
public int cruntLevel;

public bool IsGameStartedFirstTime;
public bool IsMusicOn;

public int HighSchore;

public bool []级别;

// 用于初始化
void Start(){

}


public void 保存(){
FileStream file = null ;
尝试 {
BinaryFormatter bf = new BinaryFormatter();
file = File.Create(Application.persistentDataPath + / Gamedata.data);
if (data!= null ){
data.setHighSchore(HighSchore) ;
data.setLevels(Levels);
data.SetIsGameStartedFirstTime(IsGameStartedFirstTime);
data.setIsMusicOn(IsMusicOn);
bf.Serialize(data,file); // 无法将GameData转换为System.To.Stream
}
} < span class =code-keyword> catch (例外e){}
finally {
file.Close();
};

}

[可序列化]
class GameData {
private bool IsGameStartedFirstTime;
private bool IsMusicOn;

private int HighSchore;

private bool [] Levels;

public void SetIsGameStartedFirstTime( bool IsGameStartedFirstTime)
{
this .IsGameStartedFirstTime = IsGameStartedFirstTime;
}
public bool GetIsGameStartedFirstTime()
{
return IsGameStartedFirstTime;
}
public void setIsMusicOn( bool IsMusicOn)
{
this .IsMusicOn = IsMusicOn;
}

public bool getIsMusicOn()
{
return IsMusicOn;
}

public void setHighSchore( int HighSchore)
{
this .HighSchore = HighSchore;
}

public int getHighSchore()
{
返回 HighSchore;
}

public void setLevels( bool [] Levels)
{
this .Levels = Levels;
}

public bool [] getLevels()
{
返回级别;
}
}

解决方案

尝试更改 GameData 类并使用公共属性。



 [Serializable] 
class GameData
{
public bool IsGameStartedFirstTime {获得; set ; }

public bool IsMusicOn {获得; set ; }

public int HighSchore {获得; set ; }

public bool []级别{获得; set ; }
}





[更新]

好​​吧,我以为你有一个运行时错误没有编译错误。

此行

 bf.Serialize(数据,文件); 



应该是

 bf.Serialize(文件,数据); 



(也许打开帮助???)



尝试此代码作为工作样本:

  public   void 保存()
{
尝试
{
GameData data = new GameData();
data.HighSchore = 1024 ;
data.IsGameStartedFirstTime = true ;
data.IsMusicOn = false ;
data.Levels = new bool [ 10 ];
data.Levels [ 0 ] = true ;
data.Levels [ 1 ] = true ;

使用(FileStream文件= File.Create( @ C:\Temp \ Gamedata.data))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(文件,数据); // CAN 将GameData转换为System.Stream
}

GameData dataFromFile = new GameData();
使用(FileStream文件= File.OpenRead( @ C:\ Temp \ Gamedata.data))
{
BinaryFormatter bf = new BinaryFormatter() ;
dataFromFile =(GameData)bf.Deserialize(file);
}
}
catch (例外e)
{
MessageBox.Show(e.ToString( ));
}
}


i am trying to save gamesdata in file but in gives error cannot Convert GameData to System.To.Stream

Severity Code Description Project File Line
Error CS1503 Argument 1: cannot convert from 'GameData' to 'System.IO.Stream' Assembly-CSharp-vs GameControle.cs 43


......... Help Me ........


using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; 


public class GameControler : MonoBehaviour {

    public static GameControler instance;

    private GameData data;

    public int cruntSchore;
    public int cruntLevel;

    public bool IsGameStartedFirstTime;
    public bool IsMusicOn;

    public int HighSchore;

    public bool[] Levels;

    // Use this for initialization
    void Start () {
	
	}


    public void Save() {
        FileStream file = null;
        try {
            BinaryFormatter bf = new BinaryFormatter();
            file = File.Create(Application.persistentDataPath + "/Gamedata.data");
            if(data != null){
                data.setHighSchore(HighSchore);
                data.setLevels(Levels);
                data.SetIsGameStartedFirstTime(IsGameStartedFirstTime);
                data.setIsMusicOn(IsMusicOn);
                bf.Serialize(data, file); //cannot Convert GameData to System.To.Stream 
            }
        } catch(Exception e) { }
        finally {
            file.Close();
        };

    }

[Serializable]
class GameData {
    private bool IsGameStartedFirstTime;
    private bool IsMusicOn;
    
    private int HighSchore;

    private bool[] Levels;

    public void SetIsGameStartedFirstTime(bool IsGameStartedFirstTime)
    {
        this.IsGameStartedFirstTime = IsGameStartedFirstTime;
    }
    public bool GetIsGameStartedFirstTime()
    {
        return IsGameStartedFirstTime;
    }
    public void setIsMusicOn(bool IsMusicOn)
    {
        this.IsMusicOn = IsMusicOn;
    }

    public bool getIsMusicOn()
    {
        return IsMusicOn;
    }

    public void setHighSchore(int HighSchore)
    {
        this.HighSchore = HighSchore;
    }

    public int getHighSchore()
    {
        return HighSchore;
    }

    public void setLevels(bool[] Levels)
    {
        this.Levels = Levels;
    }

    public bool[] getLevels()
    {
        return Levels;
    }
}

解决方案

Try to change your GameData class and use public properties instead.

[Serializable]
class GameData
{
    public bool IsGameStartedFirstTime { get; set; }

    public bool IsMusicOn { get; set; }

    public int HighSchore { get; set; }

    public bool[] Levels { get; set; }
}



[UPDATE]
Well, I thought you had a runtime error not a compiler error.
This line

bf.Serialize(data, file);


should be

bf.Serialize(file, data);


(Maybe open the help???)

Try this code as a working sample:

public void Save()
{
    try
    {
        GameData data = new GameData();
        data.HighSchore = 1024;
        data.IsGameStartedFirstTime = true;
        data.IsMusicOn = false;
        data.Levels = new bool[10];
        data.Levels[0] = true;
        data.Levels[1] = true;

        using (FileStream file = File.Create(@"C:\Temp\Gamedata.data"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(file, data); // CAN Convert GameData to System.Stream 
        }

        GameData dataFromFile = new GameData();
        using (FileStream file = File.OpenRead(@"C:\Temp\Gamedata.data"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            dataFromFile = (GameData)bf.Deserialize(file);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}


这篇关于将类转换为流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆