我有一个搜索过滤器,它遍历我拥有的客户列表,并且根据字符串是否包含值,返回满足条件的列表。现在它检查条件中的公司属性,但我希望它能够检查方法中给出的任何属性(customerDetail是此函数中的属性名称)。如何传递字符串属性并使其与此语句一起使用?
If(oListOfCustomers.Item(iIndex).Company.ToString.Trim.Contains(sStringContains) = True) Then
为了澄清,我想要的是:
If(oListOfCustomers.Item(iIndex).customerDetail.ToString.Trim.Contains(sStringContains) = True) Then
这是功能:
Public Shared Function GetContains(ByVal sStringContains As String, ByVal customerDetail As String, ByVal oListOfCustomers As List(Of Customer))
Dim oCustomerData As New CustomerData
Dim oNewListOfCustomers As New List(Of Customer)
Dim iIndex As Integer
Dim oCustomer As Customer
'Property Names: Company, Contact, City, State, Country, Zip, Status
'Check for all properties. It works though.
If IsNothing(oListOfCustomers) = False AndAlso oListOfCustomers.Count > 0 Then
For iIndex = 0 To oListOfCustomers.Count - 1
If (oListOfCustomers.Item(iIndex).Company.ToString.Trim.Contains(sStringContains) = True) Then
oCustomer = New Customer(oListOfCustomers.Item(iIndex).Company, oListOfCustomers.Item(iIndex).Contact, oListOfCustomers.Item(iIndex).Address1, oListOfCustomers.Item(iIndex).Address2, _
oListOfCustomers.Item(iIndex).City, oListOfCustomers.Item(iIndex).State, oListOfCustomers.Item(iIndex).Country, oListOfCustomers.Item(iIndex).Zip, _
oListOfCustomers.Item(iIndex).Zip4, oListOfCustomers.Item(iIndex).Email, oListOfCustomers.Item(iIndex).Phone, oListOfCustomers.Item(iIndex).Status, _
oListOfCustomers.Item(iIndex).MasterContactID)
oNewListOfCustomers.Add(oCustomer)
End If
Next
End If
Return oNewListOfCustomers
End Function
答案 0 :(得分:1)
您可以使用反射和PropertyInfo.GetValue
。尝试(未经测试):
Imports System.Reflection
Public Shared Function GetContains(ByVal sStringContains As String, ByVal customerDetail As String, ByVal oListOfCustomers As List(Of Customer))
Dim oCustomerData As New CustomerData
Dim oNewListOfCustomers As New List(Of Customer)
Dim iIndex As Integer
Dim oCustomer As Customer
Dim propertyInfo As PropertyInfo
'Property Names: Company, Contact, City, State, Country, Zip, Status
'Get PropertyInfo for customerDetail-property
propertyInfo = GetType(Customer).GetProperty(customerDetail)
'Check for all properties. It works though.
If IsNothing(oListOfCustomers) = False AndAlso oListOfCustomers.Count > 0 Then
For iIndex = 0 To oListOfCustomers.Count - 1
If (propertyInfo.GetValue(oListOfCustomers.Item(iIndex)).ToString.Trim.Contains(sStringContains) = True) Then
oCustomer = New Customer(oListOfCustomers.Item(iIndex).Company, oListOfCustomers.Item(iIndex).Contact, oListOfCustomers.Item(iIndex).Address1, oListOfCustomers.Item(iIndex).Address2,
oListOfCustomers.Item(iIndex).City, oListOfCustomers.Item(iIndex).State, oListOfCustomers.Item(iIndex).Country, oListOfCustomers.Item(iIndex).Zip,
oListOfCustomers.Item(iIndex).Zip4, oListOfCustomers.Item(iIndex).Email, oListOfCustomers.Item(iIndex).Phone, oListOfCustomers.Item(iIndex).Status,
oListOfCustomers.Item(iIndex).MasterContactID)
oNewListOfCustomers.Add(oCustomer)
End If
Next
End If
Return oNewListOfCustomers
End Function