/ src / settings-ui / Settings.UI.Library / GenericProperty`1.cs
GenericProperty`1.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Text.Json.Serialization;
 6  using System.Text.RegularExpressions;
 7  
 8  namespace Microsoft.PowerToys.Settings.UI.Library
 9  {
10      public class GenericProperty<T> : ICmdLineRepresentable
11      {
12          [JsonPropertyName("value")]
13          public T Value { get; set; }
14  
15          public GenericProperty(T value)
16          {
17              Value = value;
18          }
19  
20          // Added a parameterless constructor because of an exception during deserialization. More details here: https://learn.microsoft.com/dotnet/standard/serialization/system-text-json-how-to#deserialization-behavior
21          public GenericProperty()
22          {
23          }
24  
25          [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1000:Do not declare static members on generic types", Justification = "Adding ICmdLineRepresentable support")]
26          public static bool TryParseFromCmd(string cmd, out object result)
27          {
28              result = null;
29  
30              if (ICmdLineRepresentable.TryParseFromCmdFor(typeof(T), cmd, out var value))
31              {
32                  result = new GenericProperty<T> { Value = (T)value };
33                  return true;
34              }
35  
36              return false;
37          }
38  
39          public bool TryToCmdRepresentable(out string result)
40          {
41              result = Value.ToString();
42              return true;
43          }
44      }
45  }