如何通过反射在对象的可空属性上调用HasValue?

时间:2010-09-15 13:51:29

标签: vb.net reflection nullable

此函数循环对象的所有属性以创建updatequery以将te对象保存到DB。

由于引入了可空属性,我们不得不对其进行一些更改。 如果属性可以为空,我们想检查'HasValue'属性。 这有效时有效。当属性没有值时,我们在CBool​​-line上得到'非静态方法需要目标'错误

有什么建议吗? 另一种使用反射检查属性的'HasValue'-prop的方法吗?

感谢。


Private Function GetUpdateQuery(ByVal obj As Object, ByRef params As List(Of SqlParameter), Optional ByVal excl As String() = Nothing) As String
     Dim sql As String = String.Empty

     Dim props As PropertyInfo() = obj.GetType().GetProperties

     If excl Is Nothing Then
          excl = New String() {}
     End If

     For Each prop As PropertyInfo In props
          Try
               If Not excl.Contains(prop.Name) And prop.CanWrite = True Then
                    sql &= String.Format("{0} = @{1},", prop.Name, prop.Name)

                    Dim param As SqlParameter

                    Dim value As Object

                    If prop.PropertyType.IsGenericType AndAlso prop.PropertyType.GetGenericTypeDefinition() = GetType(Nullable(Of )) Then

                         If CBool(prop.PropertyType.GetProperty("HasValue").GetValue(prop.GetValue(obj, Nothing), Nothing)) Then
                              value = prop.GetValue(obj, Nothing)
                         Else
                              value = DBNull.Value
                         End If
                    Else
                         If prop.GetValue(obj, Nothing) = Nothing Then
                              value = DBNull.Value
                         Else
                              value = prop.GetValue(obj, Nothing)
                         End If
                    End If
                    param = ConnSql.CreateParameter("@" & prop.Name, value)

                    params.Add(param)
               End If
          Catch ex As Exception

          End Try

     Next

     sql = sql.Substring(0, sql.Length - 1)

     Return sql
End Function

1 个答案:

答案 0 :(得分:3)

您不需要以下If。你可以删除它。

  

If prop.PropertyType.IsGenericType AndAlso prop.PropertyType.GetGenericTypeDefinition() = GetType(Nullable(Of )) Then

但您确实需要修复以下If

If prop.GetValue(obj, Nothing) = Nothing Then

If prop.GetValue(obj, Nothing) IS Nothing Then

-

完整代码:

Private Function GetUpdateQuery(ByVal obj As Object, ByRef params As List(Of SqlParameter), Optional ByVal excl As String() = Nothing) As String
     Dim sql As String = String.Empty

     Dim props As PropertyInfo() = obj.GetType().GetProperties

     If excl Is Nothing Then
          excl = New String() {}
     End If

     For Each prop As PropertyInfo In props
          If Not excl.Contains(prop.Name) And prop.CanWrite = True Then
               sql &= String.Format("{0} = @{1},", prop.Name, prop.Name)

               Dim param As SqlParameter

               Dim value As Object

               If prop.GetValue(obj, Nothing) Is Nothing Then
                    value = DBNull.Value
               Else
                    value = prop.GetValue(obj, Nothing)
               End If

               param = ConnSql.CreateParameter("@" & prop.Name, value)

               params.Add(param)
          End If
     Next

     sql = sql.Substring(0, sql.Length - 1)

     Return sql
End Function