I have class which handles some UI stuff, lets call that class and instance UI for the example.
inside this class I will have an object of type Colors:
public class Colors {
public ConsoleColor Primary { get; set; } = ConsoleColor.White;
public ConsoleColor Default { get; set; } = ConsoleColor.Gray;
public ConsoleColor Input { get; set; } = ConsoleColor.Gray;
public ConsoleColor Success { get; set; } = ConsoleColor.Green;
public ConsoleColor Error { get; set; } = ConsoleColor.Red;
public ConsoleColor Highlight { get; set; } = ConsoleColor.Blue;
}
Inside UI I have this:
public static class UI {
public static readonly Colors Colors = new();
public static void ConfigureColors(Action<Colors> modification) {
modification.Invoke(Colors);
}
}
The problem is that I want the to be able to access the getters of the Colors property outside UI like this:
var primaryColor = UI.Colors.Primary;
but not the setters... right now, both of these work:
UI.ConfigureColors(colors => {
colors.Primary = ConsoleColor.Yellow;
});
UI.Colors.Primary = ConsoleColor.Green;
but I only want to be able to set the colors using the ConfigureColors() method. I thought it would naturally be like this since I set the Colors property to readonly, but it doesn't work like I wanted.
I tried playing around with the accessability of the properties inside the Colors class, like making them readonly or "private set" but all the modification I thought of caused both the options to not work.