为什么 List<string> 被保存在应用程序设置中,而 LinkedList<string> 不是?

问题描述

这是我的 Settings.setting 文件

 <Setting Name="AutoCompleteList" Type="System.Collections.Generic.LinkedList&lt;string&gt;" Scope="User">
      <Value Profile="(Default)" />
 </Setting>

这是我的 Settings.Designer.cs:

public global::System.Collections.Generic.LinkedList<string> AutoCompleteList {
   get {
       return ((global::System.Collections.Generic.LinkedList<string>)(this["AutoCompleteList"]));
   }
   set {
       this["AutoCompleteList"] = value;
   }
}

程序.cs

class Program
{
    private static LinkedList<string> _autoCompleteList;
    static void Main()
    {
        _autoCompleteList = new LinkedList<string>((new[] {"one","two"}));
        
        Settings.Default.AutoCompleteList = _autoCompleteList;
        Settings.Default.Save(); //nothing is saved. (Silently and without any error)
        
        _autoCompleteList = Settings.Default.AutoCompleteList; //null
    }
}

如果我使用 List<string> 而不是 LinkedList<string> 一切正常;但我需要 LinkedList,因为它可以更轻松地访问上一个和下一个元素。是的。可以在应用设置中使用 List 并在运行时将其加载到 LinkedList 中,但我想知道为什么 LinkedList 无法保存在应用设置中。

此外,我应该明确指出我的所有设置都属于“用户”范围

解决方法

链表是一种仅在内存中实现的数据结构,因为每个元素还链接到另一个元素。这就是它无法保存的原因。它只能在内存中实现。