添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
打酱油的香蕉  ·  Merged Heme and ...·  1 月前    · 
追风的大象  ·  ASP.NET ...·  1 月前    · 
豪爽的香烟  ·  RPM resource ...·  2 月前    · 
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. 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) and not get them by category name.
vb.net Code:
  1. Dim _Form As Form = CType(Me, Form)
  2. Dim ListOfPropertyNames As New List(Of String) From {"BackColor", "ForeColor"}
  3. For Each _Property In ListOfPropertyNames
  4.     Dim _PropertyDescriptor As PropertyDescriptor = TypeDescriptor.GetProperties(_Form)(_Property)
  5.     If _PropertyDescriptor.CanResetValue(_Form) Then
  6.         If _PropertyDescriptor.GetValue(_Form) IsNot Nothing Then
  7.             _PropertyDescriptor.ResetValue(_Form)
  8.         End If
  9.     End If
  10. Next
Just looking at the documentation for the PropertyDescriptor class I can see that it has an Attributes property. Without having tested anything, I would guess you could use something like this:
vb.net Code:
  1. Dim category = DirectCast(_PropertyDescriptor.Attributes(GetType(CategoryAttribute)), CategoryAttribute)
  2.  
  3. If category.Category = someParticularCategoryName AndAlso _PropertyDescriptor.CanResetValue(_Form) Then
vb.net Code:
  1. Dim PropertyCollection As List(Of PropertyDescriptor) = TypeDescriptor.
  2.                                                         GetProperties(_Control).
  3.                                                         OfType(Of PropertyDescriptor).
  4.                                                         Where(Function(item) item.Category = "Appearance").
  5.                                                         OrderBy(Function(item) item.Category).
  6.                                                         ThenBy(Function(item) item.DisplayName).
  7.                                                         ToList()
Edited after jmcilhinney 's correction below !!! Ha! The Category property is right below the Attributes property in the documentation and I didn't even notice it because I was specifically looking for attributes. Mind you, it does mention the CategoryAttribute in the description of the Category property, so I probably should have seen it.
As for your code, it may not matter anyway as the LINQ provider may optimise it but it seems to me that the Where should come before the OrderBy and, given that you're filtering by Category, you should be sorting only by DisplayName. Have you read something that indicates that sorting before filtering is preferred? If you consider the amount of work that would be done if each of those three operations were done literally as written then 'OrderBy(Function(item) item.Category).ThenBy(Function(item) item.DisplayName).Where(Function(item) item.Category = "Appearance")' would be much less efficient than 'Where(Function(item) item.Category = "Appearance").OrderBy(Function(item) item.DisplayName)'.
As for your code, it may not matter anyway as the LINQ provider may optimise it but it seems to me that the Where should come before the OrderBy and, given that you're filtering by Category, you should be sorting only by DisplayName. Have you read something that indicates that sorting before filtering is preferred? If you consider the amount of work that would be done if each of those three operations were done literally as written then 'OrderBy(Function(item) item.Category).ThenBy(Function(item) item.DisplayName).Where(Function(item) item.Category = "Appearance")' would be much less efficient than 'Where(Function(item) item.Category = "Appearance").OrderBy(Function(item) item.DisplayName)'.
Hm, you are right! And it does the "work" even if I use it without OrderBy and ThenBy .
vb.net Code:
  1. Dim PropertyCollection As List(Of PropertyDescriptor) = TypeDescriptor.
  2.                                                         GetProperties(_Control).
  3.                                                         OfType(Of PropertyDescriptor).
  4.                                                         Where(Function(item) item.Category = "Appearance").
  5.                                                         ToList()
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.