我的发电机中有这个功能。
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
谢谢。