I am working on a C# application and I want to support multiple themes, so I wrote a ThemeManager
class that can check, load and apply a specific theme. A theme is considered valid, when all of it's member variables are set (not null
). If a theme is invalid I want to throw an IncompleteThemeException
. However the following code feels a bit stupid...
public static void Load(Theme theme)
{
AccentBrush = theme.AccentBrush ?? throw new IncompleteThemeException();
AccentBrushLight = theme.AccentBrushLight ?? throw new IncompleteThemeException();
WarningBrush = theme.WarningBrush ?? throw new IncompleteThemeException();
WarningBrushLight = theme.WarningBrushLight ?? throw new IncompleteThemeException();
ErrorBrush = theme.ErrorBrush ?? throw new IncompleteThemeException();
ErrorBrushLight = theme.ErrorBrushLight ?? throw new IncompleteThemeException();
MainBackgroundBrush = theme.MainBackgroundBrush ?? throw new IncompleteThemeException();
InactiveDecoratorBrush = theme.InactiveDecoratorBrush ?? throw new IncompleteThemeException();
SecondaryBackgroundBrush = theme.SecondaryBackgroundBrush ?? throw new IncompleteThemeException();
MainTextBrush = theme.MainTextBrush ?? throw new IncompleteThemeException();
SecondaryTextBrush = theme.SecondaryTextBrush ?? throw new IncompleteThemeException();
DefaultPasswordIndicatorBrush = theme.DefaultPasswordIndicatorBrush ?? throw new IncompleteThemeException();
AccentColor = theme.AccentColor ?? throw new IncompleteThemeException();
MainBackgroundColor = theme.MainBackgroundColor ?? throw new IncompleteThemeException();
AccentColorLight = theme.AccentColorLight ?? throw new IncompleteThemeException();
}
I thought about maybe using reflection to somehow iterate over all members of the theme instance to check if one of them is null, but I'm unsure what the best solution to this would be in terms of readability, performance and extensibility in the future.
How can this be improved?