简体   繁体   English

将对象强制转换为C#

[英]cast object to class C#

Trying to cast .dat file to its own Class 试图将.dat文件强制转换为自己的类

  level0 = (Level0) LoadObjInBinary(level0, "Level" + levelNumber);

   public static object LoadObjInBinary(object myClass, string fileName) {
        fileName += ".dat";   
        if (File.Exists(FileLocation + fileName)) {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(FileLocation + fileName, FileMode.Open);
            myClass = bf.Deserialize(file);
            file.Close();
            return myClass;
        } else {
            return null;
        }
   }


Level() class

   [Serializable]
    public class Level0 { //Using this class to create Level.dat binary file 

        static int level = 1;
        static int moves = 15;
        static int seconds;
        static int minScoreForOneStar = 1000;
        static int minScoreForTwoStars = 1500;
        static int minScoreForThreeStars = 2000;

        static TargetObj[] targetObjs = {new TargetObj(Targets.Black, 10), new TargetObj(Targets.Freezer, 1), new TargetObj(Targets.Anchor, 2)}; 

        static Color[] colors = {Constants.grey, Constants.green, Constants.pink, Constants.brown, Constants.purple, Constants.lightBlue};

        static Cell[,] levelDesign;  

      //the rest is Properties of Fields

    }

Problem: LoadObjInBinary returning null. 问题:LoadObjInBinary返回null。 The file path is correct and class also matches, but don't know why "(Level0) object" not working... 文件路径是正确的,类也匹配,但不知道为什么“(Level0)对象”不工作...

Thanks 谢谢

Thank you for providing the Level0 class. 感谢您提供Level0类。

The problem is that static fields are never serialized, since they do not belong to the instance of the object you intantiate, they are global. 问题是静态字段从不被序列化,因为它们不属于您实例化的对象的实例,它们是全局的。

As I assume you will need them to be static, so they are accessible from all parts of your application, the quick work-around is to create another class with non-static members, then serialize-deserialize it, and assign it's values to the global static instance of Level0 (wherever you use it). 我假设您需要它们是静态的,因此可以从应用程序的所有部分访问它们,快速解决方法是使用非静态成员创建另一个类,然后对其进行序列化 - 反序列化,并将其值分配给Level0的全局静态实例(无论你在哪里使用它)。

[Serializable]
class Level0Data
{
    int level = 1;
    int moves = 15;
    int seconds;
    int minScoreForOneStar = 1000;
    ...
}

Then after serialization and deserialization you can do the following. 然后在序列化和反序列化之后,您可以执行以下操作。

 Level0Data deserializedObject = (Level0Data) LoadObjInBinary(..);
 Level0.level = deserializedObject.level;
 Level0.moves = deserializedObject.moves;

As you have to make sure that Level0.level, moves and all other members are public, or atleast publicly available for modification in another way. 因为你必须确保Level0.level,move和所有其他成员都是公开的,或者至少公开可以以其他方式进行修改。

Additionaly you have to make sure that 另外你必须确保这一点

class TargetObj{}
class Cell{}

Are also marked as Serializable, otherwise they will not get written on the file, nor any deserialization information regarding them. 也标记为Serializable,否则它们将不会写入文件,也不会有任何关于它们的反序列化信息。

EDIT 编辑

Here you can find all supported in Unity by default serializable types: 在这里,您可以找到Unity支持的所有可序列化类型:

Unity SerializeField Unity SerializeField

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

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