public static TypeSyntax GetCsTypeSyntax(this SemanticModel vbSemanticModel, ITypeSymbol typeSymbol, VisualBasicSyntaxNode contextNode) { if (typeSymbol.IsNullable()) { return(SyntaxFactory.NullableType(GetCsTypeSyntax(vbSemanticModel, typeSymbol.GetNullableUnderlyingType(), contextNode))); } var predefined = typeSymbol.SpecialType.GetPredefinedKeywordKind(); if (predefined != Microsoft.CodeAnalysis.CSharp.SyntaxKind.None) { return(SyntaxFactory.PredefinedType(SyntaxFactory.Token(predefined))); } var typeName = typeSymbol.ToMinimalCSharpDisplayString(vbSemanticModel, contextNode.SpanStart); return(SyntaxFactory.ParseTypeName(typeName)); }
public TypeSyntax GetTypeSyntax(TypeReference typeReference, bool isOptional) { typeReference = typeReference ?? throw new ArgumentNullException(nameof(typeReference)); if (typeReference.Primitive != null) { switch (typeReference.Primitive.Value) { case PrimitiveType.Any: return(SF.ParseTypeName("object")); case PrimitiveType.Boolean: return(NullableIfOptional(SF.ParseTypeName("bool"))); case PrimitiveType.Date: return(NullableIfOptional(SF.ParseTypeName("DateTime"))); case PrimitiveType.Json: return(SF.ParseTypeName("JObject")); case PrimitiveType.Number: return(NullableIfOptional(SF.ParseTypeName("double"))); case PrimitiveType.String: return(SF.ParseTypeName("string")); default: throw new ArgumentException($"Unexpected primitive type {typeReference.Primitive.Value}", nameof(typeReference)); } } if (typeReference.Collection != null) { TypeSyntax elementType = GetTypeSyntax(typeReference.Collection.ElementType, false); switch (typeReference.Collection.Kind) { case CollectionKind.Array: return(SF.ArrayType( elementType, SF.List(new[] { SF.ArrayRankSpecifier() }) )); case CollectionKind.Map: return(SF.ParseTypeName($"IDictionary<string, {elementType}>")); default: throw new ArgumentException($"Unexpected collection type {typeReference.Collection.Kind}", nameof(typeReference)); } } if (typeReference.Union != null) { return(SF.ParseTypeName("object")); } if (typeReference.FullyQualifiedName != null) { var type = GetTypeFromFullyQualifiedName(typeReference.FullyQualifiedName); var typeSyntax = SF.ParseTypeName(GetName(type, true)); return(type.Kind == TypeKind.Enum ? NullableIfOptional(typeSyntax) : typeSyntax); } throw new ArgumentException("Invalid type reference", nameof(typeReference)); TypeSyntax NullableIfOptional(TypeSyntax type) { return(isOptional ? SF.NullableType(type) : type); } }