Can you have a generic List(of T) in your settings file?

萝らか妹 提交于 2019-11-28 13:24:02

I assume you're talking about user settings...

I don't know if you can specify a generic type, although if you create your own non-generic type which inherits from List(of MyClass), then you can specify this type as your user setting without any problem.

Something like:

Public Class MyClassList 
    Inherits List(Of MyClass)

End Class

Then you should be able to browse for MyClassList .

I agree that Meta-Knight's inheritance solution is best, however it is possible to do this by manually editing the setting XML file. In Solution Explorer go to: Solution > Project > Properties > Settings.settings. Right-click on Settings.settings and choose "Open With". Select "XML (Text) Editor". Then insert the following inside <Settings>:

<Setting Name="TestGenericList" Type="System.Collections.Generic.List&lt;int&gt;" Scope="User">
  <Value Profile="(Default)" />
</Setting>

save, and it works.

Unfortunately it doesn't appear to work with generic types requiring two or more types, since the comma is not allowed in that attribute. Even entity-encoding it (&#x002C;) didn't help.

The following causes an error "Illegal characters in path" when you go to edit the settings file in the settings designer:

<Setting Name="TestGenericDictionary" Type="System.Collections.Generic.Dictionary&lt;string&#x002C;string&gt;" Scope="User">
  <Value Profile="(Default)" />
</Setting>
MCattle

I may be a little late to the game in providing a solution to this answer, but you could include your generic settings property in a partial class in a different file in the project, so the Settings Designer won't overwrite any custom code you add.

In my case (using VB.NET), I wanted to store a Dictionary(Of String, String) in My.Settings, except the dictionary had to be serialized as Binary because it doesn't easily serialize to XML. I removed the setting property from the designer, and then added the file Settings.Extended.vb to my project with the following code inside:

'** Keeping My.Settings.FormLayouts out of Settings.Designer.vb so that this custom code is not overwritten by Visual Studio.'

Namespace My
   Partial Friend NotInheritable Class MySettings
      <Configuration.UserScopedSettingAttribute(), _
       DebuggerNonUserCodeAttribute(), _
       Configuration.SettingsSerializeAs(Configuration.SettingsSerializeAs.Binary)> _
      Public Property FormLayouts() As Dictionary(Of String, String)
         Get
            Return CType(Me("FormLayouts"), Dictionary(Of String, String))
         End Get
         Set(ByVal value As Dictionary(Of String, String))
            Me("FormLayouts") = value
         End Set
      End Property
   End Class
End Namespace

The FormLayouts property is still part of My.Settings, and will be loaded and saved as usual, but adding more settings in the application's Settings Designer will not overwrite the code above.

You could serialize your List<T> to a string, and put that in your settings file.

Would that work?

Yes it is possible, just type List<SomeType> into the type field manually, it should work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!