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

前端 未结 5 1391
悲&欢浪女
悲&欢浪女 2020-12-11 15:23

In my settings file, I would like to have one setting that is of the type List(of Myclass).... I can\'t find anything on the net that does this. Is it possible?

相关标签:
5条回答
  • 2020-12-11 15:59

    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 .

    0 讨论(0)
  • 2020-12-11 16:01

    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.

    0 讨论(0)
  • 2020-12-11 16:05

    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>
    
    0 讨论(0)
  • 2020-12-11 16:05

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

    Would that work?

    0 讨论(0)
  • 2020-12-11 16:19

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

    0 讨论(0)
提交回复
热议问题