2017-02-22 81 views
0

我使用羅斯林創造什麼樣的應該是某種形式的數據訪問層的語言無關的代碼生成器時產生具有羅斯林以及格式化可空類型的語法。 它將使用元數據來輸出所需的代碼。預計將返回C#和VB.NET版本的代碼。生成語言無關的代碼

當前實現正在生成所需的輸出,但可空類型格式不正確,輸出包含額外的空白。

是否有確保SyntaxGenerator.NullableTypeExpression回報產生的可空類型良好的格式化選項 - 無空格 - 申報?

代碼段

這是SyntaxGenerator.NullableTypeExpression用於返回對應於該屬性類型SyntaxNode。

private SyntaxNode ToTypeExpression(Type type, bool nullable, SyntaxGenerator generator) 
    { 

     SyntaxNode baseType; 
     SyntaxNode propType; 

     SpecialType specialType = ToSpecialType(type); 

     if(specialType == SpecialType.None) 
     { 
      baseType = generator.IdentifierName(type.Name);     
     } 
     else 
     { 
      baseType = generator.TypeExpression(specialType); 
     } 

     if (nullable && type.IsValueType) 
     { 
      propType = generator.NullableTypeExpression(baseType); 
     } 
     else 
     { 
      propType = baseType; 
     } 

     return propType; 

    } 

這是生成的VB.NET代碼:

Public Property Salary As Integer? 
     Get 
      Return GetAttributeValue(Of Integer?)("salary").Value 
     End Get 

     Set(value As Integer?) 
      SetPropertyValue("Salary", "salary", value) 
     End Set 
    End Property 

這是C#代碼,注意空格

public int ? Salary 
    { 
     get 
     { 
      return GetAttributeValue<int ? >("salary").Value; 
     } 

     set 
     { 
      SetPropertyValue("Salary", "salary", value); 
     } 
    } 
+0

什麼是你的問題? –

+0

回聲Ed,這裏有什麼錯誤信息,預期條件等? –

+1

您是否嘗試過'.W ithAdditionalAnnotat離子(Formatter.Annot通貨膨脹)'? –

回答

0

使用Formatter.Format作爲建議在這裏:Generating well-formatted syntax with Roslyn ,刪除了不需要的空白。

 string textOutput; 
     var sb = new StringBuilder(); 

     var result = generator.CompilationUnit(declarations); 

     var formattedResult = Formatter.Format(result, workspace); 

     using (StringWriter writer = new StringWriter(sb)) 
     { 
      formattedResult.WriteTo(writer); 
      textOutput = writer.ToString(); 
     }