在roslyn中获取隐式转换运算符

时间:2016-09-08 16:51:30

标签: c# roslyn

我试图在roslyn中获取隐式转换运算符的符号,但是没有为代码获取它:

int? i;
i = 1;

我期待符号:

public static implicit operator Nullable<T>(T value)
{
    return new Nullable<T>(value);
}

这是我的测试代码:

internal class Walker : CSharpSyntaxWalker
{
    public SemanticModel Model { get; set; }

    public override void VisitAssignmentExpression(AssignmentExpressionSyntax node)
    {
        var o = (IAssignmentExpression)Model.GetOperation(node);
        if (o.Value.Kind == OperationKind.ConversionExpression)
        {
            var c = (IConversionExpression)o.Value;
            Console.WriteLine($"{(c.IsExplicit ? "explicit" : "implicit")} conversion from {c.Operand.ConstantValue.Value} via {(c.UsesOperatorMethod ? c.OperatorMethod.ToString() : "?")}");
        }
        base.VisitAssignmentExpression(node);
    }
}

internal static class Program
{
    private static void Main()
    {
        var tree = CSharpSyntaxTree.ParseText(@"
public class Program
{
    public static void Main()
    {
        int? i;
        i = 1;
    }
}");
        var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
        var compilation = CSharpCompilation.Create(null, new[] { tree }, new[] { mscorlib });
        var walker = new Walker { Model = compilation.GetSemanticModel(tree) };
        walker.Visit(tree.GetRoot());
    }
}

输出是:

implicit conversion from 1 via ?

我做错了什么或者是roslyn的错误?

github项目 - https://github.com/isanych/so-39396441

0 个答案:

没有答案