Is it possible to specify achieve following:
[SomeAttribute(condition1)]
public SomeType SomeSetting1 {get; set;}
[SomeAttribute(condition2)]
public SomeType SomeSetting2 {get; set;}
where condition is something complicated? To example,
[SomeAttribute(SomeSetting3 == 4 && SomeSetting4 < 100)]
I am using PropertyGrid
to show/edit configuration as properties of some serializable class. And I need to have sort of cascading: when some setting is set, some others may be hidden, depending on the value.
Currently, I can do hide some setting in this way:
- create new attribute, based on
IHide
- assign it to needed properties
check all attributes for a given property in
ConfigWrapper
, if there is any ofIHide
type, then check itsHide
to decide whenever show (add to result collection of properties) or not.public interface IHide { bool Hide { get; } } public class AdminAttribute : Attribute, Common.IHide { public bool Hide { get { return !MySettings.Admin; } } public override object TypeId { get { return "AdminAttributeId"; } } } // admin only setting [Admin] public SomeType SomeSetting {get; set;}
This way I have to add a new attribute for any new setting (which have to hide some other settings) or combinations (and this is why I want something more generic). Of course sometimes I can use attribute parameter, to be able to use one attribute for several similar purposes:
public class ElementAttribute : Attribute, Common.IHide
{
private string _element;
public bool Hide
{
get { return !Something.Instance.IsElement(_element); }
}
public ElementAttribute(string element)
{
_element = element;
}
public override object TypeId { get { return "ElementAttributeId"; } }
}
By using this attribute, I can specify element symbol:
// setting will be shown if element a present
[Element('a')]
public SomeType SomeSetting {get; set;}
After creating multiple of such, I came to idea what maybe it is possible somehow to code that Hide()
method condition into the attribute parameter itself??? Or perhaps specify behavior (action) somehow?
I can pretty easily do it by using CodeDom
me think, but it would be very sloooooow. It is possible to enumerate all attributes and cache conditions. But maybe there is an easier/alternative way? Any other ideas?
Starting bounty
I am searching for ideas to combine multiple IHide
attributes (AdminAttribute
- show settings when user is admin, ElementAttribute
- show setting when specified element presents, etc.) into a single super-attribute. I want to be able to specify conditions somehow, without the need to create new IHide
based attribute for every case.
What would be your solution if you have to handle hundreds of settings (which are existing at once), but with relations between each other and additionally related on some other conditions? How to create Admin
and Element
attribute behavior without creating AdminAttribute
and ElementAttribute
?
The point is, what there are multiple different configurations (inherited from base configuration) and I want to be able in some of them freely specify visibility conditions with a part of code, which, if evaluates into false, will hide the setting, without creating dozens of IHide
based attributes. Sort of declarative programming when defining the setting itself!
ICustomTypeDescriptor
which creates a copy of only needed properties forPropertyGrid
). What I need now is conditional attribute to control properties visibility. Something what can be easily used as declarative attribute for hundreds of properties. – Arthritis