2010-03-12 25 views
1

我在我的發生器中有這個功能。如何使用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 

感謝。

回答

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 
相關問題