2016-03-08 175 views
0

我有一個搜索過濾器,通過我有客戶的列表,並根據字符串是否包含值,返回滿足條件的列表。現在它檢查條件中的公司屬性,但是我希望它能夠檢查方法中給出的任何屬性(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 

回答

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