I am trying to list some properties names by Category attribute and put them into a variable.
For example, to get all properties names which "belong" to category Appearance and put them into a variable.
I have a similar example which resets specific properties, but I have to add them one by one which I want to avoid.
Dim _Form As Form = CType(Me, Form)
Dim ListOfPropertyNames As New List(Of String) From {"BackColor", "ForeColor"}
For Each _Property In ListOfPropertyNames
Dim _PropertyDescriptor As PropertyDescriptor = TypeDescriptor.GetProperties(_Form)(_Property)
If _PropertyDescriptor.CanResetValue(_Form) Then
If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then
_PropertyDescriptor.ResetValue(_Form)
End If
End If
Next
There are four possible scenarios in your question (that I can think of):
(More probable) Get the List of Properties ordered by
Category
andDisplayName
and, if the PropertyDescriptor.CanResetValue() method returns a positive result, reset the Property.Eventually, filter by
Category
inserting a.Where
clause.Get the List of Properties which belong to some pre-defined Categories and reset all the Properties that have a value different from the DefaultValueAttribute, checking the
PropertyDescriptor.CanResetValue()
method result.Same as above, but also adding a list of specific Properties to reset.
Get the List of Properties which belong to some pre-defined Categories and reset all the Properties no matter what
PropertyDescriptor.CanResetValue()
thinks about it.First case scenario:
Using LINQ, query the TypeDescriptor to build a list of PropertyDescriptor elements ordered by
Category
andDisplayName
.Check
PropertyDescriptor.CanResetValue()
result and reset the Property value to its default if it has changed.The same with a Category filter:
The same, grouping the Properties list by
Category
usingGroupBy()
:Second case scenario:
Using LINQ, query the
TypeDescriptor
to build a list ofPropertyDescriptor
elements related to a predefined Category list.Check
PropertyDescriptor.CanResetValue()
result and reset the Property value to its default if it has changed.Third case scenario:
Same as above, but add a filter for some specific properties:
Fourth case scenario:
Using LINQ, query the
TypeDescriptor
to build a list ofPropertyDescriptor
elements related to a predefined Category list.Check whether the
DefaultValueAttribute
of a Property isNothing
, because if it is,PropertyDescriptor.CanResetValue()
will skip it and look for a custom method alternative to get the result. Since no such methods are definined here, it will always return false unless it can detect that a Property has been changed.If no
DefaultValueAttribute
is present, orPropertyDescriptor.CanResetValue()
returnsTrue
, resets all Properties values to Default, checking beforehand that a ResetValue() method exists for the Property.