I have a class that holds a dictionary of parameters:
public class Parameter : IExecutionParameter, IDesignerParameter
{
}
public interface IExecutionSettings
{
IDictionary<string, IExecutionParameter> Parameters { get; }
}
public interface IDesignerSettings
{
IDictionary<string, IDesignerParameter> Parameters { get; }
}
public class Settings : IExecutionSettings, IDesignerSettings
{
private Dictionary<string, Parameter> _parameters;
// TODO: Implement IExecutionSettings.Parameters
// TODO: Implement IDesignerSettings.Parameters
}
I want to create explicit implementations of these interfaces (this I know how to do), but I can’t figure out how to properly cast the dictionary.
Any help will be appreciated.
You can’t, because it wouldn’t be safe. Suppose you could cast
Dictionary<string, MemoryStream>toIDictionary<string, IDisposable>– you’d then be able to put anyIDisposablevalue in the dictionary via the latter reference, even though the actual dictionary can only holdMemoryStream-compatible references.You could potentially create a readonly dictionary wrapper which delegates to an underlying dictionary for all reads, and fails on all writes. You could then wrap your
Dictionary<string, Parameter>in two of these “view” dictionaries. It would be a bit of a pain, but doable. Whether or not that’s the most appropriate approach in your case is a different matter.Perhaps instead you should have:
You could easily implement both of those (explicitly) within
Settings.