The steps below work.
However, I believe your issue is simply because xPoint
and yPoint
do not have public setters. This is due to how XmlSerializer
works. See the documentation here.
First, create a setting. In this case I named it ListOfPoints
. The type is irrelevant, we're going to change it anyways.
Manually edit "Settings.settings". I just open it with Visual Studio's XML editor but use what you prefer.
Then change the type of the setting only. Note you need to use HTML encoding for <
and >
.
Entire Settings.settings:
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsFormsApp1.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ListOfPoints" Type="System.Collections.Generic.List<WindowsFormsApp1.MyPoint>" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
The only change made was this:
Type="System.Collections.Generic.List<WindowsFormsApp1.MyPoint>"
All Code:
[Serializable]
public struct MyPoint
{
public uint X { get; set; }
public uint Y { get; set; }
public MyPoint(uint x, uint y)
{
X = x;
Y = y;
}
public override bool Equals(object obj)
{
if (!(obj is MyPoint))
return false;
var other = (MyPoint)obj;
return other.X == X && other.Y == Y;
}
public override int GetHashCode()
{
return unchecked(X.GetHashCode() ^ Y.GetHashCode());
}
}
private static readonly List<MyPoint> saveMe = new List<MyPoint>();
private static List<MyPoint> loadMe;
private static void SaveData()
{
Properties.Settings.Default.ListOfPoints = saveMe;
Properties.Settings.Default.Save();
}
private static void LoadData()
{
Properties.Settings.Default.Reload();
loadMe = Properties.Settings.Default.ListOfPoints;
TestData();
}
private static void TestData()
{
if (loadMe.Count != saveMe.Count)
throw new Exception("Different counts");
for (int i = 0; i < loadMe.Count; i++)
{
if (!loadMe[i].Equals(saveMe[i]))
throw new Exception($"{nameof(MyPoint)} at index {i} doesn't match");
}
}
Test this by adding whatever you wish to saveMe
. Then run SaveData
followed by LoadData
.
LoadData
will throw an exception if the data doesn't match.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…