2016-03-08 178 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