按类别属性将属性列表转换为变量

时间:2018-05-27 18:29:54

标签: vb.net winforms typedescriptor

我正在尝试按类别属性列出一些属性名称,并将它们放入变量。<登记/> 例如,要获取“属于”类别外观的所有属性名称,并将它们放入变量中。

我有一个类似的例子来重置特定的属性,但我必须逐个添加它们,我想避免。

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

1 个答案:

答案 0 :(得分:0)

您的问题中有四种可能的情况(我能想到):

  1. (更有可能)获取CategoryDisplayName订购的属性列表,如果PropertyDescriptor.CanResetValue()方法返回肯定结果,请重置属性。
    最后,按Category过滤.Where子句进行过滤。

  2. 获取属于某些预定义类别的属性列表,并重置所有值与DefaultValueAttribute不同的属性,并检查PropertyDescriptor.CanResetValue()方法结果。

  3. 与上述相同,但也添加了要重置的特定属性列表。

  4. 获取属于某些预定义类别的属性列表,并重置所有属性,无论PropertyDescriptor.CanResetValue()如何看待它。

  5. 第一种情况:
    使用LINQ,查询TypeDescriptor以构建按CategoryDisplayName排序的PropertyDescriptor元素列表。
    检查PropertyDescriptor.CanResetValue()结果并将属性值重置为默认值(如果已更改)。

    Dim PropertyCollection As List(Of PropertyDescriptor) = TypeDescriptor.
                                                            GetProperties(_Form).
                                                            OfType(Of PropertyDescriptor).
                                                            OrderBy(Function(item) item.Category).
                                                            ThenBy(Function(item) item.DisplayName).
                                                            ToList()
    
    For Each _PropertyDescriptor As PropertyDescriptor In PropertyCollection
        If _PropertyDescriptor.CanResetValue(_Form) Then
            If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then
                _PropertyDescriptor.ResetValue(_Form)
            End If
        End If
    Next
    

    与类别过滤器相同:

    Dim PropertyCollection As List(Of PropertyDescriptor) = TypeDescriptor.
                                                            GetProperties(_Form).
                                                            OfType(Of PropertyDescriptor).
                                                            OrderBy(Function(item) item.Category).
                                                            ThenBy(Function(item) item.DisplayName).
                                                            Where(Function(item) item.Category = "Appearance").
                                                            ToList()
    

    同样,使用Category GroupBy()Dim _Form As Form = Me Dim PropertyCollection As List(Of IGrouping(Of String, PropertyDescriptor)) = TypeDescriptor. GetProperties(_Form). OfType(Of PropertyDescriptor). OrderBy(Function(item) item.Category). ThenBy(Function(item) item.DisplayName). GroupBy(Function(item) item.Category). ToList() 'Extract one Category. It could also be a second For Each loop. Dim CategoryPropertyList As List(Of PropertyDescriptor) = PropertyCollection. SelectMany(Function(grp) grp). Where(Function(prop) prop.Category = "Appearance"). ToList() For Each _PropertyDescriptor As PropertyDescriptor In CategoryPropertyList If _PropertyDescriptor.CanResetValue(_Form) Then If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then _PropertyDescriptor.ResetValue(_Form) End If End If Next 对属性列表进行分组:

    TypeDescriptor

    第二种情况:
    使用LINQ,查询PropertyDescriptor以构建与预定义类别列表相关的PropertyDescriptor.CanResetValue()元素列表。
    检查Dim _Form As Form = CType(Me, Form) Dim ListOfCategoryNames As New List(Of String) From {"Appearance", "Behavior"} For Each Category As String In ListOfCategoryNames Dim _PropertyCollection As List(Of PropertyDescriptor) = TypeDescriptor. GetProperties(_Form). OfType(Of PropertyDescriptor). Where(Function(item) item.Category = Category). ToList() For Each _PropertyDescriptor As PropertyDescriptor In _PropertyCollection If _PropertyDescriptor.CanResetValue(_Form) Then If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then _PropertyDescriptor.ResetValue(_Form) End If End If Next Next 结果并将属性值重置为默认值(如果已更改)。

    Dim ListOfCategoryNames As New List(Of String) From {"Appearance", "Behavior"}
    Dim ListOfPropertyNames As New List(Of String) From {"BackColor", "ForeColor", "AllowDrop", "Enabled"}
    
    For Each Category As String In ListOfCategoryNames
        For Each PropertyName As String In ListOfPropertyNames
            Dim _PropertyDescriptor As PropertyDescriptor = TypeDescriptor.GetProperties(_Form).
                                                            OfType(Of PropertyDescriptor).
                                                            Where(Function(item) item.Category = Category AndAlso
                                                                                 item.Name = PropertyName).
                                                            FirstOrDefault()
    
            If (_PropertyDescriptor IsNot Nothing) AndAlso _PropertyDescriptor.CanResetValue(_Form) Then
                If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then
                    _PropertyDescriptor.ResetValue(_Form)
                End If
            End If
        Next
    Next
    

    第三种情况:
    与上面相同,但为某些特定属性添加过滤器:

    TypeDescriptor

    第四种情况:
    使用LINQ,查询PropertyDescriptor以构建与预定义类别列表相关的DefaultValueAttribute元素列表。
    检查属性的Nothing是否为PropertyDescriptor.CanResetValue(),因为 如果是,DefaultValueAttribute将跳过它并寻找自定义方法替代以获得结果。由于此处没有确定此类方法,因此除非可以检测到属性已更改,否则它将始终返回false 如果不存在PropertyDescriptor.CanResetValue(),或True返回Dim _Form As Form = CType(Me, Form) Dim ListOfCategoryNames As New List(Of String) From {"Appearance", "Behavior"} For Each Category As String In ListOfCategoryNames Dim _PropertyCollection As List(Of PropertyDescriptor) = TypeDescriptor. GetProperties(_Form). OfType(Of PropertyDescriptor). Where(Function(item) item.Category = Category). ToList() For Each _PropertyDescriptor As PropertyDescriptor In _PropertyCollection Dim _DefaultAttribute As DefaultValueAttribute = _PropertyDescriptor. Attributes.OfType(Of DefaultValueAttribute). Where(Function(item) item.IsDefaultAttribute). FirstOrDefault If (_DefaultAttribute Is Nothing) Or (_PropertyDescriptor.CanResetValue(_Form)) Then If _PropertyDescriptor.GetType().GetMethod("ResetValue", BindingFlags.Public Or BindingFlags.Instance) IsNot Nothing Then If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then _PropertyDescriptor.ResetValue(_Form) End If End If End If Next Next ,则将所有属性值重置为默认值,事先检查属性是否存在ResetValue()方法。
    < / p>

    	<form action='recordresult.php' method='POST' name='form_filter' class="form-style-1" >    
        <b>Search</b><br>
        <select name="selectVal">
            <option value="category" >Select a category</option>
    <option value="first_name">First Name</option>
    <option value="surname">Surname</option>
    <option value="address">Address</option>
    <option value="phonenumber">Telephone</option>
        </select>
        <input type='text' name='search' placeholder='Enter text here...'><br>
        <input type='submit' value='Send'>  
    </form>