1

我的发电机中有这个功能。

    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

如果开发人员为“​​boundedValue”参数传入小数,为“numericType”参数传入小数类型,则会生成以下代码。

Public Const DollarAmountMaximumValue As Decimal = 100000

尽管传递给 CodePrimitiveExpression 对象的构造函数的数据类型是小数,但生成的代码是一个整数,它被隐式转换并存储在一个小数变量中。有没有办法让它在数字后用“D”生成,如下所示:

Public Const DollarAmountMaximumValue As Decimal = 100000D

谢谢。

4

1 回答 1

0

好吧,我对这个解决方案不满意,但除非有人有更好的解决方案,否则我将不得不采用它。

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
于 2010-03-12T14:51:11.667 回答