Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SemanticModel.GetSymbolInfo(typeSyntax) returns null

I am having problems resolving some types in Roslyn. Most of the times it works as expected But in some occasions, in some files, some types fail to resolve. This is the code that I use to handle types:

private string GetTypeName(TypeSyntax type)
{
    var si = _model.GetSymbolInfo(type);
    var s = si.Symbol;
    if (s == null)
    {
        var ti = _model.GetTypeInfo(type);
        if (ti.Type?.Kind == SymbolKind.ErrorType)
        {
            return "**error type**";
        }

        return "**unknown type**";
    }
    return GetTypeNameFromSymbol(s as ITypeSymbol);
}

I pass a TypeSyntax from various different expressions and declarations. It seems to be per document and per type failure. In some documents it cannot resolve Task as a type, and in some it cannot resolve different exception types.

I am unable to create a small example that reproduces this. I use the above code in my C# to Kotlin transpiler: https://github.com/rogeralsing/ProjectExodus

In some cases I end up in the "error type" branch. And in some in the "unknown type"

And the failure occurs only when trying to transpile some fairly large solutions from what I can see so far.

What can cause this? I get my semantic model from the document like so:

var model = await d.GetSemanticModelAsync();
var root = await d.GetSyntaxRootAsync();

Is there something else I should do?

EDIT: I've managed to extract this information: It says the containing assembly is missing. Why is that? the source solution opens and builds fine in VS.NET

enter image description here

like image 884
Roger Johansson Avatar asked Oct 17 '25 20:10

Roger Johansson


1 Answers

Check if you have the same class-file saved as e.g "class1.old" and you are loading it in your prj/solution workspace. In that case it will create ambiguity!

like image 123
Genci Ymeri Avatar answered Oct 19 '25 11:10

Genci Ymeri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!