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」パラメーターに 10 進数を渡し、「numericType」パラメーターに 10 進数型を渡すと、次のコードが生成されます。

Public Const DollarAmountMaximumValue As Decimal = 100000

CodePrimitiveExpression オブジェクトのコンストラクターに渡されるデータ型は 10 進数ですが、生成されるコードは整数であり、暗黙的に変換されて 10 進数変数に格納されます。次のように、数字の後に「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 に答える