This project is read-only.
1
Vote

User-defined conversion classification

description

I expect the program below to print ImplicitUserDefined, but instead it prints ImplicitNullable. Even if it is correct in the conversion being a nullable conversion, it needs to have its MethodSymbol member set (which it doesn't).
class Program {
    static void Main() {
        var syntaxTree = CSharpSyntaxTree.ParseText(@"
                         class C {
                             public static implicit operator int(C c) { return 0; }
                             public C() {
                                 int? i = this;
                             }
                         }");

        var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { new MetadataFileReference(typeof(object).Assembly.Location) });
        var semanticModel = compilation.GetSemanticModel(syntaxTree);
        var root = syntaxTree.GetRoot();
        var value = root.DescendantNodes().OfType<ConstructorDeclarationSyntax>().First().Body.Statements.OfType<LocalDeclarationStatementSyntax>().Single().Declaration.Variables[0].Initializer.Value;
        var conversion = semanticModel.GetConversion(value);
        Console.WriteLine(conversion);
    }
}

comments