Monday, May 20, 2024

C# Get JSON property name


using System.Text.Json.Serialization;
class TestClass {

[JsonPropertyName("test-id")]
public string TestId { get; set; }

public static string GetName(string name) => typeof(CompanyInfo).GetProperty(name)?.GetCustomAttribute<JsonPropertyAttribute>()?.Name ?? name;

}

using Newtonsoft.Json;
class TestClass {

[JsonPropertyName("test-id")]
public string TestId { get; set; }

public static string GetName(string name) => typeof(CompanyInfo).GetProperty(name)?.GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName ?? name;

}

Use as:
TestClass.GetName(nameof(TestClass.GetName))

Turn on Windows 11 Fast Boot

If windows starting is slow, to enable windows 11 fast startup/boot,  Press Windows + R, type powercfg.cpl, and hit Enter.  This will direct...