5

I want to validate the the property which is visible in the project only and that's a default namespace value on compile action. I was able to find ability to validate project references but this does not provide option to read the default project's namespace.

Is it possible to do with Roslyn Analyzer?

Edit: I found that during Compilation start there is available some property but this does not give wanted information:

context.Compilation.GlobalNamespace.IsNamespace > returns true
context.Compilation.GlobalNamespace.IsGlobalNamespace > returns true
context.Compilation.GlobalNamespace.Name > returns ""
context.Compilation.GlobalNamespace.ToDisplayString() > returns "<global namespace>"

I know that I can go to each class and check the namespace but this is not the case since I want to know the default project's namespace. An example: project name="Project.UnitTests", assembly="Project.UnitTests" and default namespace is "Project.UnitTest". The default namespace is not matching project since project name is plural and namespace is single meaning.

8
  • You can do it with MSBuild. Commented Mar 24, 2021 at 11:49
  • How? Any examples? Commented Mar 24, 2021 at 14:03
  • What validation do you want to perform? Commented Mar 24, 2021 at 15:53
  • I want to check for default global namespace only and show compiler error if violated rule. The same way as any other diagnostic error shown in Visual studio. Same way as I did for the references check for the project. Commented Mar 25, 2021 at 6:14
  • The MSBuild Error Task causes a build error. Commented Mar 25, 2021 at 9:33

1 Answer 1

5

In my case, it is for C# Source Generator, is not for Analyzer, but anyway I was succeeded in getting the root namespace of the target project in my source generator.

The document of detail is here:

https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#consume-msbuild-properties-and-metadata

At first, you have to declare <CompilerVisibleProperty> item in the item group of the target project file (it is not a source generator project file) to allow your source generator to retrieve the specified MSBUild property value. In this case, you should specify the RootNamespace property.

...
<!-- This is the target application's .csproj file -->
<ItemGroup>
  <CompilerVisibleProperty Include="RootNamespace" />
</ItemGroup>
...

After that, you can get the root namespace by the code as below, instead of referencing context.Compilation.GlobalNamespace.

...
// This is C# Source Generator's .cs file.
[Generator]
public class MySourceGenerator : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
        ...

I'm not sure that this code can apply also C# Analyzor, but I hope this information helpful for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.