英文:
Is it possible to serialize this?
问题
我有这些类。
Store 是最小的部分,但它包含所有数据。
我还有 Game,它有 Store 的列表。
Store 还有 Progressbar、Labels、Button,如何序列化和反序列化,使每个对象与特定的控件和数据对应。
英文:
I have these classes.
Store is the smallest piece, but it holds all the data.
I also have Game which has List of stores.
Store also has Progressbar,Labels,Button, how can I (de)serialize it so that each object corresponds with the specific controls and data.
[Serializable]
public class Game
{
public static int Money { get; set; } = 111111110;
public static int Gold { get; set; } = 250;
public List<Store> Stores { get; set; }
[Serializable]
public class Store
{
public string Name { get; set; }
public int Interval { get; set; }
public int Num_Upgrades { get; set; } = 0;
public double BaseCost { get; set; }
public int BaseMoney { get; set; }
public int MoneyMaking { get; set; }
public int BaseInterval { get; set; }
public int CostOfUpgrade { get; set; }
public int IntervalReducer { get; set; } = 1;
public int EarnMultiplier { get; set; } = 1;
public CustomProgressBar ProgressBar;
public Button UpgradeButton;
public System.Windows.Forms.Timer Timer;
public Home HomeForm { get; set; }
public Label Label { get; set; }
public Label EarnLabel { get; set; }
public bool Clicked { get; set; } = false;
public bool IsBought { get; set; }
public bool hasManager { get; set; } = false;
Random random = new Random();
public static Game game { get; set; }
public Home()
{
InitializeComponent();
this.DoubleBuffered = true;
Deserialize();
}
I mean it works but all I have is this Image
答案1
得分: 1
我会假设你的 "store" 类是某种 UI 类,它有按钮等。
你最好不要尝试序列化任何 UI 类,因为 UI 类附带了大量逻辑,这往往会使序列化/反序列化变得复杂。这对于大多数包含大量逻辑的其他类也是如此。
你可能应该创建专门用于数据传输的单独类,通常称为 DTOs。这些类是没有任何逻辑的。然后,你可以使用库对你的 DTO 进行序列化,例如使用 Json,但如果你喜欢其他东西,也有很多其他选择。
根据你的设计,可能需要一些代码来从 DTO 复制值到你的 UI 或反之,尽管这可能会增加代码量,但编写和维护这样的代码往往非常简单。
英文:
I would assume your "store" class is some type of UI class, it it has buttons etc.
You should most likely not try to serialize any UI classes, since UI classes has a whole bunch of logic attached to them, and this tend to complicate serialzation/deserialization. This is also true for most other classes that contain large amounts of logic.
What you should probably do is to create separate classes specifically for Data Transfer, often called DTOs. These are classes without any logic at all. You can then use a library to serialize your DTOs, for example using Json, but there are plenty of other alternatives if you prefer something else.
Depending on your design it might require some code to copy values from your DTO to your UI or vice versa, and while this might increase the amount of code, it tend to be very straightforward code to write and maintain.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论