I have an Interface with a bunch of properties, some of which are defined as readonly, eg:
public interface IActivity {
string Id { get; }
bool IsEnabled { get; }
}
The class that implements this interface has public set methods, for use elsewhere, but the consumer of this interface should not be able to set these properties. I'm now building a test consumer, and exposing the object through a PropertyGrid control. Because it's binding to the class with the public set methods, the properties which are readonly as far as the consumer should be concerned are editable within the grid.
I can fix this problem in two ways, firstly by making the setters internal, or by marking the properties with the [ReadOnly] attribute, however this doesn't seem "right", since in theory something else could implement this interface out of my control, with those supposedly readonly properties then changeable via the property grid.
I tried explicitly casting to the interface when assigning the object to the property grid, but that didn't help either:
propGrid.SelectedObject = (IActivity)obj;
Is there a way to force a PropertyGrid control to respect the contract of the Interface, rather than having to change the concrete classes?
SelectedObjectexpects anobjectanyway. - poke