我正在尝试使用Roslyn导航到方法声明。我有命名空间名称,类名和方法名。在下面的代码示例中,我可以填充classReference变量。但是一旦调用了classReference.GetMembers(),我就会得到Method Not Found Exception。虽然当我在中间窗口(Visual Studio调试模式)中执行classReference.GetMembers()时,它给了我正确的方法名称。
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;
}
}
}
异常 system.missingMethodException而
的HResult = 0x80131513
Message =找不到方法:'System.Collections.Immutable.ImmutableArray`1 Microsoft.CodeAnalysis.INamespaceOrTypeSymbol.GetMembers()'。
这令人困惑。当我查看Microsoft.CodeAnalysis.INamespaceOrTypeSymbol文档时,GetMembers()方法存在(公共),我正在引用具有此方法的程序集,它也可以在运行时使用。
请注意 classReference.GetMembers(methodName)仅适用于中级窗口,不确定发生了什么。
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>
答案 0 :(得分:1)
问题是System.Collections.Immutable版本,我使用的是1.5.0预览,因为我在nuget包管理器UI中点击了更新全部。
<package id="System.Collections.Immutable" version="1.5.0-preview1-26216-02" targetFramework="net461" />
一旦我将 System.Collections.Immutable 降级到版本1.3.1,异常就消失了。