Is a nullable type not a struct or value type?

已答复 Is a nullable type not a struct or value type?

  • 2012년 1월 3일 화요일 오전 12:41
     
      코드 있음

    I wrote the following code to determine the type of 'int?' using Roslyn:-

    static void Main(string[] args)
            {
                var compilationUnit = SyntaxTree.ParseCompilationUnit(@"
    class Class
    {
    int? a = 10;
    }");
                AssemblyFileReference mscorlib = new AssemblyFileReference(typeof(object).Assembly.Location);
                Compilation compilation = Compilation.Create("RoslynExp2"nullnew[] { compilationUnit }, new[] { mscorlib });
                SemanticModel semanticModel = compilation.GetSemanticModel(compilationUnit);
                FieldDeclarationSyntax fieldDecl = compilationUnit.Root.DescendentNodes().OfType<FieldDeclarationSyntax>().First();
                FieldSymbol fieldSymbol = semanticModel.GetDeclaredSymbol(fieldDecl) as FieldSymbol;
                var type = fieldSymbol.Type;
                Console.WriteLine("field name = {0}, type name = {1}, type kind = {2}", fieldSymbol.Name, type.Name, type.TypeKind);
                Console.WriteLine(String.Format("TypeKind.Struct = {0}, IsValueType = {1}", type.TypeKind == TypeKind.Struct, type.IsValueType));
            }

    And I got the following output:-

     

    field name = a, type name = Nullable, type kind = Error

    TypeKind.Struct = False, IsValueType = False

    Is this expected? Or did I make a mistake while writing the code?

     


    Nikhil Agarwal

모든 응답

  • 2012년 1월 3일 화요일 오후 6:50
     
     답변됨

    The TypeKind was Error.  I doubt nullable type notation (?) is supported in the preview.


    Wayward LINQ Lacky
    • 답변으로 표시됨 NikhilAgarwal 2012년 1월 4일 수요일 오전 2:35
    •  
  • 2012년 1월 3일 화요일 오후 10:05
    소유자
     
     

    Hi Nikhil - Nullable types are not supported in the Roslyn CTP. You can find the list of known issues and not-yet-implemented features here.

     

    As Matt mentioned, you can check the TypeSymbol.Kind / .TypeKind to ensure that the returned symbol is good.


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team