2

I have color scheme for my code editor in a settings file in the project. I have a several similar settings files each containing different color scheme.

To make those settings selectable on runtime, I need them to implement ColorScheme interface.

So far so good, code works fine, with only one major annoyance: each time settings are changed, the interface part is removed from Designer file, so the code doesn't find them anymore.

Is there a way to force code generator to add my interface to generated class? Or is it other workaround for this? I tried to make designer file readonly, but then I see lots of annoying VS dialogs.

Without the interface, I can't cast settings class on anything. I could probably read its properties via Reflection, but this approach looks like an ugly hack.

Harry
  • 4,524
  • 4
  • 42
  • 81

1 Answers1

6

You could go for an "extension" using a partial class for your settings, that include the interface :) (It should be in the same namespace/assembly as your settings file is). Any changes to the settings do not interfere with your self created partial class

public interface IHaveInterface
{
    void Hallo();
}

internal partial class Settings : IHaveInterface
{
    public void Hallo()
    {
        Console.WriteLine("Hallo");
    }
}

after which i can access the hallo inside the Properties.Settings.Default

Properties.Settings.Default.Hallo();
Icepickle
  • 12,689
  • 3
  • 34
  • 48