如何使用 CodeDom 创建十进制常量?

发布于 2024-08-24 21:28:21 字数 926 浏览 5 评论 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
            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

谢谢。

I have this function in my generator.

    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

If a developer passes a decimal in for the "boundedValue" parameter and the decimal type for the "numericType" parameter the following code gets generated.

Public Const DollarAmountMaximumValue As Decimal = 100000

Despite the data type being passed into the constructor of the CodePrimitiveExpression object being a decimal, the code generated is an integer that gets implicitly converted and stored in a decimal variable. Is there any way to get it to generate with the "D" after the number as in:

Public Const DollarAmountMaximumValue As Decimal = 100000D

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2024-08-31 21:28:22

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

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

Well, I'm not happy about this solution but unless someone has a better one I'll have to go with it.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文