我试图在C#中创建一个项目,其中用户输入他们的预算信息以及他们的交易(记录金额和类别,以确定他们是在预算之下还是超出预算)。
我有一个自定义事务类:
class Transaction
{
private int id;
private int year;
private int month;
private int day;
private string memo;
private string category;
private decimal amount;
public Transaction(int tranid, int tranyear, int tranmonth, int tranday,
string tranmemo, string trancategory, decimal tranamount)
{
id = tranid;
year = tranyear;
month = tranmonth;
day = tranday;
memo = tranmemo;
category = trancategory;
amount = tranamount;
}
}
在整个项目中,我决定尝试使用Visual附带的内置持久设置功能来创建一个"Transaction“类型的List对象,如下所示。
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Budget.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="transactions" Type="System.Collections.Generic.List<Transaction>" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
由于列表对象在默认情况下没有初始化-在main中,所以我使用:
Properties.Settings.Default.transactions = new List<Budget.Transaction>();
Properties.Settings.Default.transactions.Add(
new Transaction(1, 2017, 7, 27, "Birthday transaction", "Parties", 100.00m));
Properties.Settings.Default.Save();
它初始化对象并允许我完美地写入和读取列表项。在这一点上一切都正常运作。当保存持久设置时,会发生此问题。当列表是自定义类类型时,我已经隔离了这个问题。因为列表在字符串类型时容易而正确地保存。
试图保存设置会导致以下user.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Budget.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<Budget.Properties.Settings>
<setting name="transactions" serializeAs="Xml">
<value />
</setting>
</Budget.Properties.Settings>
</userSettings>
</configuration>
最后,我的问题是..。有可能做到这一点吗?是否可以使用内置的持久设置功能来保存具有自定义类类型的列表对象?还是只有在使用string或int等内置数据类型时才可行?
发布于 2017-05-20 01:41:02
您遇到的问题是您的事务对象不可序列化。为了保存文件中的事务详细信息,必须更改类以公开公共属性,而不是私有字段。我不确定您是否也需要将类标记为可序列化的,我认为它应该通过拥有公共属性来工作。
public class Transaction
{
public int id { get; set; }
public int year { get; set; }
public int month { get; set; }
public int day { get; set; }
public string memo { get; set; }
public string category { get; set; }
public decimal amount { get; set; }
}
编辑:检查后,拥有公共字段也可以。
可以序列化的项
但是.
这并不是存放事务数据的最佳场所。您可以使用相同的技术(序列化为XML)并将它们保存到单独的文件中,或者使用适当的方式存储此类数据,即数据库。我强烈建议您将Linq用于SQL。您可以了解如何创建数据库,以及如何使用Linq到SQL 这里。它本质上是一个轻量级实体框架。
发布于 2017-05-19 23:53:53
您希望在Settings
部分中的类型如下:
System.Collections.Generic.List`1[[WindowsFormsApp1.Transaction WindowsFormsApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
除了用Transaction
类的全名替换括号内的所有内容之外。
也许有一种更优雅的方法来获得全名。我只是很懒。我会在我的应用程序中放一行代码
var x = typeof(List<Transaction>).FullName;
设置一个断点。
https://stackoverflow.com/questions/44079722
复制相似问题