Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

C# -> VB: Target typed new does not convert

オープン
#1,112 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp, visualbasic
領域
tooling

調査の方向性

失敗するターゲット型付き宣言 System.IO.StreamWriter tw = new("runfile.txt"); と、CommonConversions.RemodelVariableDeclaration 内のスタックトレースのエントリポイント VisitLocalDeclarationStatement から始めます。issue の C# 入力を使って変換を再現し、ネストされたコンストラクターとターゲット型付き new が例外なしに変換され、有効な VB 出力が生成されることを確認します。

索引モデルが issue の本文から書いたものです。

説明

C# -> VB
Input code
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

namespace CMM_AutoPostProcess
{
    public class GetTargets2
    {
        public void Targets(string file)
        {
            string JobID;
            int FileNameStart;
            int FileNameLength;
            int row, i;
            bool closed;

            bool autoProcess;
            string Autofile;
            string? L;
            string Job;

            FileNameStart = file.LastIndexOf('\\') + 1;
            FileNameLength = file.Length - FileNameStart;
            JobID = file.Substring(FileNameStart, FileNameLength);

            // Convert to Uppercase
            JobID = JobID.ToUpper();

            // Find JobID in list and then extract relevant information
            i = 1;
            row = 0;
            do
            {
                try
                {
                    Job = "Test";

                    if (JobID.Contains(Job))
                    {
                        row = i;
                        autoProcess = true;
                    }
                    i++;
                }
                catch
                {
                    //Safe to skip
                }
            }
            while (row == 0);

            const string target = "Mesh";
            autoProcess = File.Exists(target);

            if (autoProcess)
            {
                Autofile = "Post.txt";
                var sw = new StreamWriter(Autofile);

                sw.WriteLine("$TEXT");
                sw.WriteLine("Endjob");
                sw.WriteLine("Execute");
                sw.WriteLine("$TXTEND");

                sw.Close();

                if (File.Exists(Autofile))
                {
                    var reader = new StreamReader(Autofile);
                    closed = false;
                    do
                    {
                        L = reader.ReadLine();

                        if (L == "$TXTEND")
                        {
                            closed = true;
                        }
                    }
                    while (!closed && reader.Peek() != -1);

                    reader.Close();
                    StreamWriter tw = new("runfile.txt");
                    string msg;

                    try
                    {
                        if (closed)
                        {
                            tw.WriteLine("Running software for " + JobID);
                            string AppRef = Path.Combine(
                                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                                @"Microsoft\Windows\Start Menu\Programs\Zeiss BG VisionCare\CMMProcess.appref-ms");
                            if (File.Exists(AppRef))
                            {
                                new Process
                                {
                                    StartInfo = new ProcessStartInfo(AppRef)
                                    {
                                        UseShellExecute = true,
                                        CreateNoWindow = true,
                                        Arguments = Autofile
                                    }
                                }.Start();
                            }
                            else
                            {
                                msg = "Cannot find click-once application " + AppRef;
                                tw.WriteLine(msg);
                                MessageBox.Show(msg);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                         MessageBox.Show(ex.ToString());
                    }
                    finally
                    {
                        tw.Close();
                    }
                }
            }
            else
            {
                var sw = new StreamWriter("PostError.txt");
                sw.WriteLine("Did not manage to find any target information");
                sw.Close();
            }
        }
    }
}
Erroneous output
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Windows.Forms

Namespace CMM_AutoPostProcess
    Public Class GetTargets2
        Public Sub Targets(file As String)
            Dim JobID As String
            Dim FileNameStart As Integer
            Dim FileNameLength As Integer
            Dim row, i As Integer
            Dim closed As Boolean

            Dim autoProcess As Boolean
            Dim Autofile As String
            Dim L As String?
            Dim Job As String

            FileNameStart = file.LastIndexOf("\"c) + 1
            FileNameLength = file.Length - FileNameStart
            JobID = file.Substring(FileNameStart, FileNameLength)

            ' Convert to Uppercase
            JobID = JobID.ToUpper()

            ' Find JobID in list and then extract relevant information
            i = 1
            row = 0
            Do
                Try
                    Job = "Test"

                    If JobID.Contains(Job) Then
                        row = i
                        autoProcess = True
                    End If
                    i += 1
                Catch
                    'Safe to skip
                End Try
            Loop While row = 0

            Const target = "Mesh"
            autoProcess = IO.File.Exists(target)

            If autoProcess Then
                Autofile = "Post.txt"
                Dim sw = New StreamWriter(Autofile)

                sw.WriteLine("$TEXT")
                sw.WriteLine("Endjob")
                sw.WriteLine("Execute")
                sw.WriteLine("$TXTEND")

                sw.Close()

                If IO.File.Exists(Autofile) Then
                    Dim reader = New StreamReader(Autofile)
                    closed = False
                    Do
                        L = reader.ReadLine()

                        If Equals(L, "$TXTEND") Then
                            closed = True
                        End If
                    Loop While Not closed AndAlso reader.Peek() <> -1

                    reader.Close()
                    ''' Cannot convert LocalDeclarationStatementSyntax, System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
                    '''    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
                    '''    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
                    '''    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
                    '''    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)
                    ''' 
                    ''' Input:
                    '''                     System.IO.StreamWriter tw = new("runfile.txt");
                    ''' 
                    ''' 
                    Dim msg As String

                    Try
                        If closed Then
                            tw.WriteLine("Running software for " & JobID)
                            Dim AppRef = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft\Windows\Start Menu\Programs\Zeiss BG VisionCare\CMMProcess.appref-ms")
                            If IO.File.Exists(AppRef) Then
                                Call New Process With {
                                        .StartInfo = New ProcessStartInfo(AppRef) With {
        .UseShellExecute = True,
        .CreateNoWindow = True,
        .Arguments = Autofile
    }
}.Start()
                            Else
                                msg = "Cannot find click-once application " & AppRef
                                tw.WriteLine(msg)
                                MessageBox.Show(msg)
                            End If
                        End If
                    Catch ex As Exception
                        Call MessageBox.Show(ex.ToString())
                    Finally
                        tw.Close()
                    End Try
                End If
            Else
                Dim sw = New StreamWriter("PostError.txt")
                sw.WriteLine("Did not manage to find any target information")
                sw.Close()
            End If
        End Sub
    End Class
End Namespace

Expected output
In the above you can see a problem converting nested constructors just after "Call New Process With". Easy to fix with a local variable for StartInfo and un-nesting constructors.

There is also a problem with converting

System.IO.StreamWriter tw = new("runfile.txt");

but that is very new syntax. The exceptions seem to relate to this problem where there is no type after the new.
Details
  • Product in use: VS extension
  • Version in use: 9.2.5.0 VS2022
  • Did you see it working in a previous version, which? First try on this code
    Converting 1 files...

Phase 1 of 2:

  • MediaLibCore\Util\GetTarget2.cs - conversion started
  • MediaLibCore\Util\GetTarget2.cs - conversion has errors:
    System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)

Phase 2 of 2:

  • MediaLibCore\Util\GetTarget2.cs - simplification started
  • MediaLibCore\Util\GetTarget2.cs - simplification has errors:
    System.InvalidCastException: Unable to cast object of type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.EmptyStatementSyntax' to type 'Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax'.
    at ICSharpCode.CodeConverter.VB.CommonConversions.RemodelVariableDeclaration(VariableDeclarationSyntax declaration)
    at ICSharpCode.CodeConverter.VB.MethodBodyExecutableStatementVisitor.VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
    at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node)
    at ICSharpCode.CodeConverter.VB.CommentConvertingMethodBodyVisitor.DefaultVisit(SyntaxNode node)

Code conversion completed with 1 error
1 files have been written to disk.
Please report issues at https://github.com/icsharpcode/CodeConverter/issues

主要言語
C#
スター
913
フォーク
244
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

icsharpcode/CodeConverter のほかの issue

icsharpcode/CodeConverter の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。