Atc.CodeAnalysis.CSharp
3.0.174
dotnet add package Atc.CodeAnalysis.CSharp --version 3.0.174
NuGet\Install-Package Atc.CodeAnalysis.CSharp -Version 3.0.174
<PackageReference Include="Atc.CodeAnalysis.CSharp" Version="3.0.174" />
<PackageVersion Include="Atc.CodeAnalysis.CSharp" Version="3.0.174" />
<PackageReference Include="Atc.CodeAnalysis.CSharp" />
paket add Atc.CodeAnalysis.CSharp --version 3.0.174
#r "nuget: Atc.CodeAnalysis.CSharp, 3.0.174"
#:package Atc.CodeAnalysis.CSharp@3.0.174
#addin nuget:?package=Atc.CodeAnalysis.CSharp&version=3.0.174
#tool nuget:?package=Atc.CodeAnalysis.CSharp&version=3.0.174
Atc.CodeAnalysis.CSharp
Target Frameworks: netstandard2.0, net9.0, net10.0
Roslyn-based utilities for programmatically working with C# code. This library provides factories and extensions for generating C# syntax trees, making it easier to create code generators, analyzers, and source generators. Multi-targeting enables use in .NET Framework 4.6.1+ projects and modern .NET applications.
Why Use This Library?
When building source generators, code generators, or code analysis tools, you often need to programmatically create C# syntax trees using Roslyn. This library simplifies that process by providing:
- Syntax Factories: Easy-to-use factory methods for creating common C# syntax nodes
- Extensions: Helper methods for working with existing syntax trees
- Type-Safe API: Strongly-typed methods that reduce boilerplate code
- Fluent Interface: Chain operations together for cleaner code generation
Perfect for:
- Source generators
- Code migration tools
- API client generators
- Code analysis tools
- Custom Roslyn analyzers
Installation
dotnet add package Atc.CodeAnalysis.CSharp
Target Frameworks
- .NET Standard 2.0
- .NET 9.0
The library multi-targets to support:
- .NET Framework 4.6.1+ (for Roslyn analyzers and source generators)
- .NET Core 2.0+
- .NET 5+
- .NET 9.0
Key Dependencies
- Microsoft.CodeAnalysis.CSharp (Roslyn)
- System.ComponentModel.Annotations
Code Documentation
Examples
Creating Class Declarations
Public Partial Class
using Atc.CodeAnalysis.CSharp.SyntaxFactories;
var classDeclaration = SyntaxClassDeclarationFactory.CreateAsPublicPartial("MyClass");
Output:
public partial class MyClass
{
}
Class with Base Type and Interface
var classDeclaration = SyntaxClassDeclarationFactory.Create(
"MyClass",
"BaseClass",
new[] { "IMyInterface", "IDisposable" });
Output:
public class MyClass : BaseClass, IMyInterface, IDisposable
{
}
Creating Attributes
Simple Attribute
using Atc.CodeAnalysis.CSharp.SyntaxFactories;
var attribute = SyntaxAttributeFactory.Create("Obsolete");
Output:
[Obsolete]
Attribute with Arguments
var attribute = SyntaxAttributeFactory.Create(
"GeneratedCode",
SyntaxArgumentFactory.Create("MyGenerator"),
SyntaxArgumentFactory.Create("1.0.0"));
Output:
[GeneratedCode("MyGenerator", "1.0.0")]
SuppressMessage Attribute
using Atc.CodeAnalysis.CSharp.Factories;
var attribute = SuppressMessageAttributeFactory.Create(
"Design",
"CA1034:NestedTypesShouldNotBeVisible",
"Justification for suppression");
Output:
[SuppressMessage("Design", "CA1034:NestedTypesShouldNotBeVisible", Justification = "Justification for suppression")]
Creating Parameters
using Atc.CodeAnalysis.CSharp.SyntaxFactories;
var parameter = SyntaxParameterFactory.Create("string", "name");
Output:
string name
Parameter with Default Value
var parameter = SyntaxParameterFactory.CreateWithDefaultValue(
"int",
"count",
SyntaxLiteralExpressionFactory.Create(10));
Output:
int count = 10
Creating Literal Expressions
using Atc.CodeAnalysis.CSharp.SyntaxFactories;
// String literal
var stringLiteral = SyntaxLiteralExpressionFactory.Create("Hello World");
// Integer literal
var intLiteral = SyntaxLiteralExpressionFactory.Create(42);
// Boolean literal
var boolLiteral = SyntaxLiteralExpressionFactory.CreateTrue();
// Null literal
var nullLiteral = SyntaxLiteralExpressionFactory.CreateNull();
Complete Example: Generating a Simple Class
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Atc.CodeAnalysis.CSharp.SyntaxFactories;
// Create a compilation unit
var compilationUnit = SyntaxFactory.CompilationUnit();
// Add using directives
compilationUnit = compilationUnit.AddUsings(
SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")));
// Create namespace
var @namespace = SyntaxFactory.NamespaceDeclaration(
SyntaxFactory.ParseName("MyNamespace"));
// Create class with attributes
var classDeclaration = SyntaxClassDeclarationFactory
.CreateAsPublicPartial("MyGeneratedClass")
.AddAttributeLists(
SyntaxAttributeListFactory.Create(
SyntaxAttributeFactory.Create("GeneratedCode",
SyntaxArgumentFactory.Create("MyGenerator"),
SyntaxArgumentFactory.Create("1.0.0"))));
// Add the class to the namespace
@namespace = @namespace.AddMembers(classDeclaration);
// Add the namespace to the compilation unit
compilationUnit = compilationUnit.AddMembers(@namespace);
// Output the code
var code = compilationUnit.NormalizeWhitespace().ToFullString();
Console.WriteLine(code);
Output:
using System;
namespace MyNamespace
{
[GeneratedCode("MyGenerator", "1.0.0")]
public partial class MyGeneratedClass
{
}
}
Working with Interpolated Strings
using Atc.CodeAnalysis.CSharp.SyntaxFactories;
var interpolated = SyntaxInterpolatedFactory.Create(
"The value is: ",
"myVariable");
Output:
$"The value is: {myVariable}"
Advanced Usage
Extending Class Declarations
The library's extension methods make it easy to work with existing syntax trees:
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Atc.CodeAnalysis.CSharp.Extensions;
ClassDeclarationSyntax classDecl = /* existing class declaration */;
// Check if class has a specific attribute
bool hasAttribute = classDecl.HasAttribute("Obsolete");
// Get all public methods
var publicMethods = classDecl.GetPublicMethods();
Contributing
Contributions are welcome! Please see the main repository README for contribution guidelines.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.3.0)
- System.ComponentModel.Annotations (>= 5.0.0)
-
net10.0
- Microsoft.CodeAnalysis.CSharp (>= 5.3.0)
- System.ComponentModel.Annotations (>= 5.0.0)
-
net9.0
- Microsoft.CodeAnalysis.CSharp (>= 5.3.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Atc.CodeAnalysis.CSharp:
| Package | Downloads |
|---|---|
|
Atc.Rest.ApiGenerator
Atc.Rest.ApiGenerator is a WebApi C# code generator using a OpenApi 3.0.x specification YAML file. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.174 | 74 | 6/24/2026 |
| 3.0.173 | 74 | 6/24/2026 |
| 3.0.172 | 73 | 6/24/2026 |
| 3.0.67 | 135 | 4/25/2026 |
| 3.0.46 | 122 | 4/15/2026 |
| 3.0.45 | 118 | 4/10/2026 |
| 3.0.44 | 116 | 4/9/2026 |
| 3.0.43 | 115 | 4/9/2026 |
| 3.0.41 | 108 | 4/9/2026 |
| 3.0.40 | 116 | 4/9/2026 |
| 3.0.18 | 166 | 2/9/2026 |
| 3.0.16 | 308 | 12/15/2025 |
| 3.0.12 | 251 | 11/28/2025 |
| 3.0.9 | 409 | 11/21/2025 |
| 3.0.8 | 342 | 11/14/2025 |
| 3.0.4 | 291 | 11/6/2025 |
| 2.0.562 | 443 | 9/4/2025 |
| 2.0.561 | 298 | 9/4/2025 |
| 2.0.560 | 306 | 9/3/2025 |
| 2.0.558 | 259 | 8/22/2025 |