How do I call HasValue on a NULL property of an object through reflection?

This function overrides all the properties of the object to create an updatequery to save the te object in the database.

We had to make some changes to it due to the introduction of properties with a zero value. If the property is null, we would like to check the "HasValue" property. It works when it matters. When the property does not matter, we get a "Non-Static Method Requiring Target Error" on the CBool ​​line

Any suggestions? Another way to check the "HasValue" property of a property using reflection?

Thank.


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
+3
source share
1 answer

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
+3

Source: https://habr.com/ru/post/1764891/


All Articles