|
4 | 4 | using Microsoft.CodeAnalysis.CSharp; |
5 | 5 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
6 | 6 | using Microsoft.CodeAnalysis.Editing; |
| 7 | +using Microsoft.CodeAnalysis.Simplification; |
7 | 8 | using static Microsoft.CodeAnalysis.VisualBasic.VisualBasicExtensions; |
8 | 9 | using ICSharpCode.CodeConverter.Util.FromRoslyn; |
9 | 10 | using ISymbolExtensions = ICSharpCode.CodeConverter.Util.ISymbolExtensions; |
@@ -729,13 +730,19 @@ private static (BaseMethodDeclarationSyntax MethodBlock, BlockSyntax ConvertedSt |
729 | 730 |
|
730 | 731 | private static ExpressionSyntax BuildCharExpressionFromVbSyntax(VBSyntax.ExpressionSyntax defaultValueNode, SemanticModel semanticModel) |
731 | 732 | { |
732 | | - // For constant char literals (e.g. "^"c), reconstruct directly from the constant value |
733 | | - var constant = semanticModel.GetConstantValue(defaultValueNode); |
734 | | - if (constant.HasValue && constant.Value is char c) { |
735 | | - return CS.SyntaxFactory.LiteralExpression(CS.SyntaxKind.CharacterLiteralExpression, CS.SyntaxFactory.Literal(c)); |
| 733 | + // For char literal expressions (e.g. "^"c), use the constant value directly |
| 734 | + if (defaultValueNode.IsKind(VBasic.SyntaxKind.CharacterLiteralExpression)) { |
| 735 | + var constant = semanticModel.GetConstantValue(defaultValueNode); |
| 736 | + if (constant.HasValue && constant.Value is char c) { |
| 737 | + return CS.SyntaxFactory.LiteralExpression(CS.SyntaxKind.CharacterLiteralExpression, CS.SyntaxFactory.Literal(c)); |
| 738 | + } |
736 | 739 | } |
737 | | - // For named constant references (e.g. DlM), use the VB expression text as-is |
738 | | - return ValidSyntaxFactory.IdentifierName(defaultValueNode.ToString()); |
| 740 | + // For named constant references (e.g. DlM or Module.DlM), build a member access expression. |
| 741 | + // Strip VB's "Global." prefix (VB global namespace qualifier, has no C# identifier equivalent). |
| 742 | + // Annotate for simplification so Roslyn reduces e.g. TestModule.DlM → DlM within TestModule. |
| 743 | + var parts = defaultValueNode.ToString().Trim().Split('.'); |
| 744 | + if (parts.Length > 1 && parts[0] == "Global") parts = parts.Skip(1).ToArray(); |
| 745 | + return ValidSyntaxFactory.MemberAccess(parts).WithAdditionalAnnotations(Simplifier.Annotation); |
739 | 746 | } |
740 | 747 |
|
741 | 748 | private static bool IsThisResumeLayoutInvocation(StatementSyntax s) |
|
0 commit comments