question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Global using support for `CSharpCompilationOptions`

See original GitHub issue

Could 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:closed
  • Created 2 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
CyrusNajmabadicommented, Feb 21, 2022

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!

1reaction
StefHcommented, Dec 22, 2022

@WeihanLi A possible workaround would be to add these to the code:

// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

More info here : https://stackoverflow.com/questions/72904348/roslyn-csharpcompilation-with-global-implicit-usings

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found