I am trying to use Roslyn to navigate to method declaration. I have Namespace name, class name and method name. Below in the code sample I am able to get classReference variable populated. But as soon as classReference.GetMembers() is called I am getting Method Not Found Exception. Although when I execute classReference.GetMembers() in intermediate window (Visual Studio debug mode) it gives me correct method names.
public async void NavigateToMethod(string className, string methodName)
{
Microsoft.CodeAnalysis.Solution sol = Workspace.CurrentSolution;
foreach (Microsoft.CodeAnalysis.Project project in sol.Projects)
{
bool found = false;
Compilation compilation = await project.GetCompilationAsync();
INamedTypeSymbol classReference = compilation.GetTypeByMetadataName(className);
List<ISymbol> l = classReference.GetMembers().ToList<ISymbol>();
Debug.WriteLine(l);
foreach (ISymbol isymbol in classReference.GetMembers(methodName))
{
IMethodSymbol methodSymbol = isymbol as IMethodSymbol;
CancellationTokenSource cts = new CancellationTokenSource();
if (Workspace.TryGoToDefinition(methodSymbol, project, cts.Token))
{
found = true;
break;
}
}
if (found)
{
break;
}
}
}
Exception System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'System.Collections.Immutable.ImmutableArray`1 Microsoft.CodeAnalysis.INamespaceOrTypeSymbol.GetMembers()'.
This is confusing. When I looked at the Microsoft.CodeAnalysis.INamespaceOrTypeSymbol documentation the GetMembers() method exists (public) and I am referencing the assembly which has this method, also it is available at runtime.
Just note classReference.GetMembers(methodName) only works in Intermediate window, not sure what is going on.
package.config
<packages>
<package id="Microsoft.CodeAnalysis" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="2.6.0" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.CodeAnalysis.Common" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.CSharp" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.VisualBasic" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.Composition" version="1.0.31" targetFramework="net461" />
<package id="Microsoft.Net.Compilers" version="2.7.0" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.VisualStudio.CoreUtility" version="15.6.27740" targetFramework="net461" />
<package id="Microsoft.VisualStudio.ImageCatalog" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Imaging" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.LanguageServices" version="2.7.0" targetFramework="net461" />
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6071" targetFramework="net461" />
<package id="Microsoft.VisualStudio.SDK.EmbedInteropTypes" version="15.0.16" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.15.0" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Framework" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop" version="7.10.6072" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.10.0" version="10.0.30320" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.11.0" version="11.0.61031" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.12.0" version="12.0.30111" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.15.3.DesignTime" version="15.0.26929" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.15.6.DesignTime" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.8.0" version="8.0.50728" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop.9.0" version="9.0.30730" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Text.Data" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.TextManager.Interop" version="7.10.6071" targetFramework="net461" />
<package id="Microsoft.VisualStudio.TextManager.Interop.8.0" version="8.0.50728" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Threading" version="15.6.46" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Threading.Analyzers" version="15.6.46" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Utilities" version="15.6.27413" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Validation" version="15.3.32" targetFramework="net461" />
<package id="Microsoft.VSSDK.BuildTools" version="15.6.170" targetFramework="net461" developmentDependency="true" />
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net461" />
<package id="StreamJsonRpc" version="1.2.8" targetFramework="net461" />
<package id="System.AppContext" version="4.3.0" targetFramework="net46" />
<package id="System.Collections" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Concurrent" version="4.3.0" targetFramework="net46" />
<package id="System.Collections.Immutable" version="1.5.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Composition" version="1.2.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Composition.AttributedModel" version="1.2.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Composition.Convention" version="1.2.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Composition.Hosting" version="1.2.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Composition.Runtime" version="1.2.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Composition.TypedParts" version="1.2.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Console" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.Debug" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.FileVersionInfo" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.StackTrace" version="4.3.0" targetFramework="net46" />
<package id="System.Diagnostics.Tools" version="4.3.0" targetFramework="net46" />
<package id="System.Dynamic.Runtime" version="4.3.0" targetFramework="net46" />
<package id="System.Globalization" version="4.3.0" targetFramework="net46" />
<package id="System.IO.Compression" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Linq" version="4.3.0" targetFramework="net46" />
<package id="System.Linq.Expressions" version="4.3.0" targetFramework="net46" />
<package id="System.Reflection" version="4.3.0" targetFramework="net46" />
<package id="System.Reflection.Metadata" version="1.6.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Reflection.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.Resources.ResourceManager" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Handles" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net46" />
<package id="System.Runtime.Numerics" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net461" />
<package id="System.Security.Cryptography.Encoding" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Security.Cryptography.X509Certificates" version="4.3.2" targetFramework="net461" />
<package id="System.Text.Encoding" version="4.3.0" targetFramework="net46" />
<package id="System.Text.Encoding.CodePages" version="4.5.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Text.Encoding.Extensions" version="4.3.0" targetFramework="net46" />
<package id="System.Threading" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Tasks.Parallel" version="4.3.0" targetFramework="net46" />
<package id="System.Threading.Thread" version="4.3.0" targetFramework="net46" />
<package id="System.ValueTuple" version="4.5.0-preview1-26216-02" targetFramework="net461" />
<package id="System.Xml.ReaderWriter" version="4.3.1" targetFramework="net461" />
<package id="System.Xml.XDocument" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XmlDocument" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XPath" version="4.3.0" targetFramework="net46" />
<package id="System.Xml.XPath.XDocument" version="4.3.0" targetFramework="net46" />
</packages>
The problem was System.Collections.Immutable version, I was using 1.5.0-preview that happened because I clicked on update all in nuget package manager UI.
<package id="System.Collections.Immutable" version="1.5.0-preview1-26216-02" targetFramework="net461" />
As soon I downgraded System.Collections.Immutable to version 1.3.1 the exception was gone.
User contributions licensed under CC BY-SA 3.0