Roslyn AdhocWorkspace asking for reference to System.Runtime

Roslyn AdhocWorkspace asking for reference to System.Runtime



I'm trying to do semantic analysis on a bit of C# code.
When setting up an AdhocWorkspace for my tests I've found examples that point to using the the assembly location on Type and object.
When I do this I get the following errors in my diagnostics:


AdhocWorkspace


Type


object



[0]: TestIsNotInstanceOfTypeMessage.cs(13,20): error CS0012: The type 'Object' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.



[1]: TestIsNotInstanceOfTypeMessage.cs(13,13): error CS0012: The type 'Type' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.



When I add the following line everything works well:


MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFrameworkv4.6.1\Facades\System.Runtime.dll"),



I'm not really happy with a hard coded path in my test project but am unsure how to "get" the 4.6.1 versions of object and Type any other way.
The code below shows the gist of my helper to get an AdhocWorkspace


object


Type


AdhocWorkspace


private static readonly ImmutableArray<MetadataReference> _coreReferences =
ImmutableArray.Create<MetadataReference>(
MetadataReference.CreateFromFile(typeof(Type).Assembly.Location),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)


private static readonly Project _baseProject = new AdhocWorkspace()
.AddProject("Test", LanguageNames.CSharp)
.AddMetadataReferences(_coreReferences)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));



Some more details:


Visual Studio Community v15.4.0


Target framework: .NET 4.6.1


Microsoft.CodeAnalysis.CSharp v2.4.0




1 Answer
1



This is due to the fact that you are using AdhocWorkspace. This workspace is designed to be .Net-agnostic as far as I know. Meaning - it should work for both .Net Standard and .Net Core, cross version.



If you want to skip issues related to default libraries, you may be better off with MSBuildWorkspace



However, this workspace relies on MSBuild being installed on the machine, and some other MSBuild specific things which is likely to lead to some other issues. Like:



Msbuild failed when processing the file 'C:PathMyfile.csproj' with message: C:Pathpackagessomepackagebuildsomepackage.targets: (75, 2): The "sometask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.



If you want to stick to AdHoc workspace - you will have to locate the references manually, by using the registry to locate the .Net assemblies folder or maybe turn to the GAC. In other words - if you want to tie the AdHoc space in your solution to the .Net Standard - you're allowed to do so, but you are on your own.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)