Skip to content

Commit b6ff932

Browse files
VB -> C#: Convert date literals - fixes #159
1 parent 3f24c1b commit b6ff932

File tree

5 files changed

+401
-348
lines changed

5 files changed

+401
-348
lines changed

ICSharpCode.CodeConverter/CSharp/CommonConversions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text;
45
using ICSharpCode.CodeConverter.Shared;
56
using ICSharpCode.CodeConverter.Util;
67
using Microsoft.CodeAnalysis;
@@ -183,6 +184,11 @@ internal ExpressionSyntax GetLiteralExpression(object value, string fullText = n
183184
if (value is char)
184185
return SyntaxFactory.LiteralExpression(Microsoft.CodeAnalysis.CSharp.SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal((char)value));
185186

187+
if (value is DateTime dt) {
188+
var valueToOutput = dt.Date.Equals(dt) ? dt.ToString("yyyy-MM-dd") : dt.ToString("yyyy-MM-dd HH:mm:ss");
189+
return SyntaxFactory.ParseExpression("DateTime.Parse(\"" + valueToOutput + "\")");
190+
}
191+
186192

187193
throw new ArgumentOutOfRangeException(nameof(value), value, null);
188194
}

ICSharpCode.CodeConverter/CSharp/NodesVisitor.cs

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.CodeAnalysis;
88
using Microsoft.CodeAnalysis.CSharp;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
using SyntaxFactory = Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1011
using VBSyntax = Microsoft.CodeAnalysis.VisualBasic.Syntax;
1112
using VBasic = Microsoft.CodeAnalysis.VisualBasic;
1213
using SyntaxToken = Microsoft.CodeAnalysis.SyntaxToken;
@@ -821,17 +822,32 @@ public override CSharpSyntaxNode VisitParameter(VBSyntax.ParameterSyntax node)
821822
returnType = arrayType.WithElementType(SyntaxFactory.NullableType(arrayType.ElementType));
822823
}
823824
}
824-
EqualsValueClauseSyntax @default = null;
825-
if (node.Default != null) {
826-
@default = SyntaxFactory.EqualsValueClause((ExpressionSyntax)node.Default?.Value.Accept(TriviaConvertingVisitor));
827-
}
825+
828826
var attributes = node.AttributeLists.SelectMany(ConvertAttribute).ToList();
829827
int outAttributeIndex = attributes.FindIndex(a => a.Attributes.Single().Name.ToString() == "Out");
830828
var modifiers = CommonConversions.ConvertModifiers(node.Modifiers, TokenContext.Local);
831829
if (outAttributeIndex > -1) {
832830
attributes.RemoveAt(outAttributeIndex);
833831
modifiers = modifiers.Replace(SyntaxFactory.Token(SyntaxKind.RefKeyword), SyntaxFactory.Token(SyntaxKind.OutKeyword));
834832
}
833+
834+
EqualsValueClauseSyntax @default = null;
835+
if (node.Default != null) {
836+
if (node.Default.Value is VBSyntax.LiteralExpressionSyntax les && les.Token.Value is DateTime dt)
837+
{
838+
var dateTimeAsLongCsLiteral = CommonConversions.GetLiteralExpression(dt.Ticks, dt.Ticks + "L");
839+
var dateTimeArg = CommonConversions.CreateAttributeArgumentList(SyntaxFactory.AttributeArgument(dateTimeAsLongCsLiteral));
840+
var optionalDateTimeAttributes = new[] {
841+
SyntaxFactory.Attribute(SyntaxFactory.ParseName("System.Runtime.InteropServices.Optional")),
842+
SyntaxFactory.Attribute(SyntaxFactory.ParseName("System.Runtime.CompilerServices.DateTimeConstant"), dateTimeArg)
843+
};
844+
attributes.Insert(0,
845+
SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(optionalDateTimeAttributes)));
846+
} else {
847+
@default = SyntaxFactory.EqualsValueClause(
848+
(ExpressionSyntax)node.Default.Value.Accept(TriviaConvertingVisitor));
849+
}
850+
}
835851

836852
if (node.Parent.Parent is VBSyntax.MethodStatementSyntax mss
837853
&& mss.AttributeLists.Any(HasExtensionAttribute) && node.Parent.ChildNodes().First() == node) {

0 commit comments

Comments
 (0)