CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Interop method invocations with OptionalAttribute aren't converted

Open TymurGubayev opened this issue 7 months ago • 2 comments

VB.Net input code

    Sub S()
        Dim what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine

        Dim objRange As Microsoft.Office.Interop.Word.Range
        objRange = objRange.GoTo(what, , -1)
        objRange = objRange.GoTo(what, Nothing, -1)
    End Sub

where the signature of the GoTo method is

    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
    [DispId(173)]
    [return: MarshalAs(UnmanagedType.Interface)]
    Range GoTo([Optional][In][MarshalAs(UnmanagedType.Struct)] ref object What, [Optional][In][MarshalAs(UnmanagedType.Struct)] ref object Which, [Optional][In][MarshalAs(UnmanagedType.Struct)] ref object Count, [Optional][In][MarshalAs(UnmanagedType.Struct)] ref object Name);

Erroneous output

        public void S()
        {
            var what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;

            var objRange = default(Microsoft.Office.Interop.Word.Range);
            object argWhat = what;
            object argCount = -1;
            objRange = default
#error Cannot convert InvocationExpressionSyntax - see comment for details
   /* Cannot convert ArgumentListSyntax, System.InvalidOperationException: Operation is not valid due to the current state of the object.
      at Microsoft.CodeAnalysis.VisualBasic.Symbols.ParameterSymbol.get_ExplicitDefaultValue()
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.CreateOptionalRefArg(IParameterSymbol p, RefKind refKind)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<GetAdditionalRequiredArgs>d__132.MoveNext()
      at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
      at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SeparatedList[TNode](IEnumerable`1 nodes)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitArgumentList>d__55.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
      at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext()

   Input: (what, , -1)

   Context:
   objRange.[GoTo](what, , -1)

    */
   ;
            what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat);
            object argWhat1 = what;
            object argWhich = null;
            object argCount1 = -1;
            objRange = default
#error Cannot convert InvocationExpressionSyntax - see comment for details
   /* Cannot convert ArgumentListSyntax, System.InvalidOperationException: Operation is not valid due to the current state of the object.
      at Microsoft.CodeAnalysis.VisualBasic.Symbols.ParameterSymbol.get_ExplicitDefaultValue()
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.CreateOptionalRefArg(IParameterSymbol p, RefKind refKind)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<GetAdditionalRequiredArgs>d__132.MoveNext()
      at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
      at Microsoft.CodeAnalysis.CSharp.SyntaxFactory.SeparatedList[TNode](IEnumerable`1 nodes)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitArgumentList>d__55.MoveNext()
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
      at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext()

   Input: (what, Nothing, -1)

   Context:
   objRange.[GoTo](what, Nothing, -1)

    */
   ;
            what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat1);
        }
    }

Expected output

public void S()
{
    var what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;

    var objRange = default(Microsoft.Office.Interop.Word.Range);
    object argWhat = what;
    object argWhich = null;
    object argCount = -1;
    objRange = default
    objRange.[GoTo](what, argWhich, -1);
    what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat);
    object argWhat1 = what;
    object argWhich1 = null;
    object argCount1 = -1;
    objRange = default
    objRange.[GoTo](what, argWhich1, -1);
    what = (Microsoft.Office.Interop.Word.WdGoToItem)Conversions.ToInteger(argWhat1);
}

Details

  • Product in use: VS extension
  • Version in use: ae25cdc920df68755fb233934028091c97b42853
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.

TymurGubayev avatar Nov 29 '23 07:11 TymurGubayev