如何使用 CodeDom 创建十进制常量?
我的生成器中有这个功能。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我对这个解决方案并不满意,但除非有人有更好的解决方案,否则我将不得不采用它。
Well, I'm not happy about this solution but unless someone has a better one I'll have to go with it.