Question: Global using support for `CSharpCompilationOptions`
See original GitHub issueCould we use the global usings feature when compiling source code?
It seemed the CSharpCompilationOptions.WithUsings
method would not work as expected.
Sample code:
var code = @"
public record Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime UpdatedAt { get; set; }
}
";
var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, new CSharpParseOptions(LanguageVersion.Latest));
var references = new[]
{
typeof(object).Assembly,
Assembly.Load("netstandard"),
Assembly.Load("System.Runtime"),
}
.Select(assembly => assembly.Location)
.Distinct()
.Select(l => MetadataReference.CreateFromFile(l))
.Cast<MetadataReference>()
.ToArray();
var assemblyName = $"DbTool.DynamicGenerated.{GuidId.NewGuid()}";
var compilation = CSharpCompilation.Create(assemblyName)
.WithOptions(
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
// support only for script? https://stackoverflow.com/questions/38795447/roslyn-effect-of-csharpcompileroptions-withusings
.WithUsings(new[]
{
"System",
"System.Collections.Generic",
"System.IO",
"System.Linq",
"System.Net.Http",
"System.Threading",
"System.Threading.Tasks"
})
)
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);
using var ms = new MemoryStream();
var compilationResult = compilation.Emit(ms);
I would get an error as follows:
The type or namespace name ‘DateTime’ could not be found (are you missing a using directive or an assembly reference?)
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (9 by maintainers)
Top Results From Across the Web
C# 10: Disable Global Using
The question asks how to disable C# 10's global using directive feature, whereby prefixing a using directive with global applies the ...
Read more >Is there a way to enable "Implicit Usings" feature when ...
The "ImplicitUsings" feature allows code to omit usings statements for standard namespaces. Is there a way to tell a C# compilation to ...
Read more >Let's Explore Global Usings in C# 10
Global usings are a new feature in C# 10 that give you the ability to declare a using directive over every source file...
Read more >C# 10.0 implicit global using directives
In this blog I'll show one of the new features supporting this: implicit global using directives (aka 'global imports'). The one problem ......
Read more >Runtime C# Code Compilation Revisited for Roslyn - Rick Strahl
Recently I needed to update my scripting tools that are integrated into Markdown Monster, in order to support .NET Core.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That property is for csx usings. However as you are not compiling csx files, but instead are compiling cs files, it’s not the right option. The semantics if C’s and csx are not the same. Thanks!
@WeihanLi A possible workaround would be to add these to the code:
More info here : https://stackoverflow.com/questions/72904348/roslyn-csharpcompilation-with-global-implicit-usings