vb.net从反射表达式中排除字符串

时间:2015-02-12 10:53:55

标签: vb.net reflection

我使用relection来制作动态过滤器。但是当我有一个DateTime字段时,我想从我的搜索日期中排除= Nothing。

 Dim Expr As Expression = Expression.Property(paramExpr, pinfo)
 Dim Typ = pinfo.PropertyType.FullName

 Dim CondPart As Expression = Nothing
 Dim toStr As Expression = Nothing
 If Typ.Contains("DateTime") Then
     toStr = Expression.Call(Expr, "ToString", Nothing, Expression.Constant("yyyyMMdd")) 'Here string to exclude "00010101" -> 01/01/0001
 Else
     toStr = Expression.Call(If(pinfo.PropertyType.IsValueType, Expr, Expression.Coalesce(Expr, Expression.Constant(String.Empty))), "ToString", Nothing)
 End If
 Dim indexOf As Expression = Expression.Call(toStr, "IndexOf", Nothing, Expression.Constant(MesDonnees.FiltreGeneral), Expression.Constant(StringComparison.OrdinalIgnoreCase))
 CondPart = Expression.NotEqual(indexOf, Expression.Constant(-1))
 If conditionGbl Is Nothing Then
     conditionGbl = CondPart
 Else
     conditionGbl = Expression.OrElse(conditionGbl, CondPart)
 End If

我试着把

toStr = Expression.Call(Expr, "ToString", Nothing, Expression.Constant("yyyyMMdd"))
toStr = Expression.NotEqual(toStr, Expression.Constant("00010101")) 

但我有错误

  

没有方法' IndexOf'存在于类型System.Boolean'

我认为它介于NotEqual和indexOf之间。

我也试过

 Dim NoNullDte As Expression = Expression.Call(Expr, "ToString", Nothing, Expression.Constant("yyyyMMdd"))
 toStr = Expression.Call(NoNullDte, "IndexOf", Nothing, Expression.Constant("00010101"), Expression.Constant(StringComparison.OrdinalIgnoreCase))

但是,我有错误:

  

没有方法' IndexOf'存在于类型System.Int32'

有人知道如何正确排除空日期?

1 个答案:

答案 0 :(得分:0)

强行尝试......

 Dim CondSpe As Expression = Nothing

 Dim toStr As Expression = Nothing
 If Typ.Contains("DateTime") Then
     toStr = Expression.Call(Expr, "ToString", Nothing, Expression.Constant("yyyyMMdd"))
      Dim indexOfnullDte As Expression = Expression.Call(toStr, "IndexOf", Nothing, Expression.Constant("00010101"))
      CondSpe = Expression.Equal(indexOfnullDte, Expression.Constant(-1))
  Else
      toStr = Expression.Call(If(pinfo.PropertyType.IsValueType, Expr, Expression.Coalesce(Expr, Expression.Constant(String.Empty))), "ToString", Nothing)
   End If

   Dim indexOf As Expression = Expression.Call(toStr, "IndexOf", Nothing, Expression.Constant(MesDonnees.FiltreGeneral), Expression.Constant(StringComparison.OrdinalIgnoreCase))
   Dim CondPart As Expression = Expression.NotEqual(indexOf, Expression.Constant(-1))

    If CondSpe IsNot Nothing Then CondPart = Expression.AndAlso(CondPart, CondSpe)

     If conditionGbl Is Nothing Then
         conditionGbl = CondPart
     Else
         conditionGbl = Expression.OrElse(conditionGbl, CondPart)
     End If

在datetime字段的情况下,我添加一个表达式(CondSpe)来过滤字段,我添加表达式。还在CondPart上