Pregunta

Tengo esta función en mi generador.

    Private Sub AddBoundedValue(ByVal boundedValue As Object, ByVal type As CodeTypeDeclaration, ByVal numericType As Type, name As String)

        If boundedValue IsNot Nothing Then

            Dim constant As New CodeMemberField(numericType, name)
            constant.Attributes = MemberAttributes.Const Or MemberAttributes.Public
            constant.InitExpression = New CodePrimitiveExpression(Convert.ChangeType(boundedValue, numericType))
            type.Members.Add(constant)

        End If

    End Sub

Si un desarrollador pasa un decimal en para el parámetro "boundedValue" y el tipo decimal para el "numericType" parámetro el siguiente código se genera.

Public Const DollarAmountMaximumValue As Decimal = 100000

A pesar del tipo de datos que se pasa en el constructor del objeto CodePrimitiveExpression ser un decimal, el código generado es un número entero que consigue implícitamente convierte y se almacena en una variable decimal. ¿Hay alguna manera de conseguir que se generan con la "D" después del número como en:

Public Const DollarAmountMaximumValue As Decimal = 100000D

Gracias.

¿Fue útil?

Solución

Bueno, no estoy feliz con esta solución, pero a menos que alguien tiene una mejor voy a tener que ir con él.

Private Sub AddBoundedValue(ByVal boundedValue As Object, ByVal type As CodeTypeDeclaration, ByVal numericType As Type, name As String)

    If boundedValue IsNot Nothing Then

        Dim constant As New CodeMemberField(numericType, name)
        constant.Attributes = MemberAttributes.Const Or MemberAttributes.Public
        If numericType Is GetType(Decimal) AndAlso [I detect if the language is VB.NET here] Then
            constant.InitExpression = New CodeSnippetExpression(boundedValue.ToString & "D")
        Else
            constant.InitExpression = New CodePrimitiveExpression(Convert.ChangeType(boundedValue, numericType))
        End If
        type.Members.Add(constant)

    End If

End Sub
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top