System.CodeDom 9.0.0

Prefix Reserved
dotnet add package System.CodeDom --version 9.0.0                
NuGet\Install-Package System.CodeDom -Version 9.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="System.CodeDom" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.CodeDom --version 9.0.0                
#r "nuget: System.CodeDom, 9.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.CodeDom as a Cake Addin
#addin nuget:?package=System.CodeDom&version=9.0.0

// Install System.CodeDom as a Cake Tool
#tool nuget:?package=System.CodeDom&version=9.0.0                

About

Provides functionality for dynamically generating and compiling source code using the Code Document Object Model (CodeDOM).

It allows developers to represent code in a language-agnostic format and then generate code in multiple languages, such as C# and VB.NET. The primary use cases include creating dynamic code generation tools, runtime code generation, and facilitating code analysis or transformation.

For a new modern development consider using the .NET Compiler Platform SDK, in particular Roslyn source generators.

Key Features

  • Write code using a common object model that can be translated into multiple programming languages.
  • Generate and compile code at runtime based on the CodeDOM.

How to Use

Generating and compiling C# code:

using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

// Create a new CodeCompileUnit to hold the code
var compileUnit = new CodeCompileUnit();

// Create a namespace
var codeNamespace = new CodeNamespace("MyNamespace");
compileUnit.Namespaces.Add(codeNamespace);

// Create a class
var classDeclaration = new CodeTypeDeclaration("MyClass")
{
    IsClass = true
};
codeNamespace.Types.Add(classDeclaration);

// Add a simple method to the class
var method = new CodeMemberMethod
{
    Name = "HelloWorld",
    ReturnType = new CodeTypeReference(typeof(void)),
};
classDeclaration.Members.Add(method);

var methodInvocation = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"),
                                                      "WriteLine",
                                                      new CodePrimitiveExpression("Hello, World!"));
method.Statements.Add(methodInvocation);

// Generate C# code from the CodeDOM structure
CodeDomProvider provider = new CSharpCodeProvider();

using (var writer = new StringWriter())
{
    var codeGenereationOptions = new CodeGeneratorOptions()
    {
        BlankLinesBetweenMembers = false,
        IndentString = "  ",
    };

    provider.GenerateCodeFromCompileUnit(compileUnit, writer, codeGenereationOptions);
    Console.WriteLine(writer.GetStringBuilder().ToString());
}

This example generates:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyNamespace {

  public class MyClass {
    private void HelloWorld() {
      Console.WriteLine("Hello, World!");
    }
  }
}

Main Types

The main types provided by this library are:

  • System.CodeDom.CodeObject
  • System.CodeDom.CodeCompileUnit
  • System.CodeDom.CodeNamespace
  • System.CodeDom.CodeTypeDeclaration
  • System.CodeDom.CodeMemberMethod
  • System.CodeDom.CodeTypeReference
  • System.CodeDom.CodeMethodInvokeExpression
  • System.CodeDom.CodeTypeReferenceExpression
  • System.CodeDom.CodePrimitiveExpression
  • System.CodeDom.Compiler.CodeDomProvider
  • System.CodeDom.Compiler.CodeGeneratorOptions
  • Microsoft.CSharp.CSharpCodeProvider
  • Microsoft.VisualBasic.VBCodeProvider

Additional Documentation

Feedback & Contributing

System.CodeDom is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Product 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 is compatible.  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. 
.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 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (451)

Showing the top 5 NuGet packages that depend on System.CodeDom:

Package Downloads
EntityFramework

Entity Framework 6 (EF6) is a tried and tested object-relational mapper for .NET with many years of feature development and stabilization.

System.Management

Provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure. Commonly Used Types: System.Management.ManagementClass System.Management.ManagementObject System.Management.SelectQuery

Mono.TextTemplating

Embeddable engine for the T4 templating language, a general-purpose way to generate text or code files using C#

Microsoft.Build.Tasks.Core

This package contains the Microsoft.Build.Tasks assembly which implements the commonly used tasks of MSBuild.

Microsoft.Windows.Compatibility

This Windows Compatibility Pack provides access to APIs that were previously available only for .NET Framework. It can be used from both .NET as well as .NET Standard.

GitHub repositories (124)

Showing the top 5 popular GitHub repositories that depend on System.CodeDom:

Repository Stars
microsoft/PowerToys
Windows system utilities to maximize productivity
dotnet/maui
.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
dotnet/roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
chocolatey/choco
Chocolatey - the package manager for Windows
dotnet/orleans
Cloud Native application framework for .NET
Version Downloads Last updated
9.0.0 327,722 11/12/2024
9.0.0-rc.2.24473.5 29,508 10/8/2024
9.0.0-rc.1.24431.7 16,222 9/10/2024
9.0.0-preview.7.24405.7 10,446 8/13/2024
9.0.0-preview.6.24327.7 17,395 7/9/2024
9.0.0-preview.5.24306.7 6,728 6/11/2024
9.0.0-preview.4.24266.19 6,574 5/21/2024
9.0.0-preview.3.24172.9 15,873 4/11/2024
9.0.0-preview.2.24128.5 5,239 3/12/2024
9.0.0-preview.1.24080.9 22,137 2/13/2024
8.0.0 18,085,707 11/14/2023
8.0.0-rc.2.23479.6 24,504 10/10/2023
8.0.0-rc.1.23419.4 74,118 9/12/2023
8.0.0-preview.7.23375.6 55,495 8/8/2023
8.0.0-preview.6.23329.7 17,420 7/11/2023
8.0.0-preview.5.23280.8 8,346 6/13/2023
8.0.0-preview.4.23259.5 17,452 5/16/2023
8.0.0-preview.3.23174.8 35,394 4/11/2023
8.0.0-preview.2.23128.3 32,392 3/14/2023
8.0.0-preview.1.23110.8 25,322 2/21/2023
7.0.0 40,450,121 11/7/2022
7.0.0-rc.2.22472.3 77,413 10/11/2022
7.0.0-rc.1.22426.10 68,664 9/14/2022
7.0.0-preview.7.22375.6 15,944 8/9/2022
7.0.0-preview.6.22324.4 3,774 7/12/2022
7.0.0-preview.5.22301.12 3,672 6/14/2022
7.0.0-preview.4.22229.4 21,744 5/10/2022
7.0.0-preview.3.22175.4 6,554 4/13/2022
7.0.0-preview.2.22152.2 19,927 3/14/2022
7.0.0-preview.1.22076.8 15,221 2/17/2022
6.0.0 70,396,655 11/8/2021
6.0.0-rc.2.21480.5 19,775 10/12/2021
6.0.0-rc.1.21451.13 53,259 9/14/2021
6.0.0-preview.7.21377.19 14,380 8/10/2021
6.0.0-preview.6.21352.12 15,177 7/14/2021
6.0.0-preview.5.21301.5 8,244 6/15/2021
6.0.0-preview.4.21253.7 30,018 5/24/2021
6.0.0-preview.3.21201.4 50,756 4/8/2021
6.0.0-preview.2.21154.6 9,984 3/11/2021
6.0.0-preview.1.21102.12 29,764 2/12/2021
5.0.0 70,379,340 11/9/2020 5.0.0 is deprecated because it is no longer maintained.
5.0.0-rc.2.20475.5 15,412 10/13/2020
5.0.0-rc.1.20451.14 9,907 9/14/2020
5.0.0-preview.8.20407.11 16,468 8/25/2020
5.0.0-preview.7.20364.11 38,465 7/21/2020
5.0.0-preview.6.20305.6 142,761 6/25/2020
5.0.0-preview.5.20278.1 13,345 6/10/2020
5.0.0-preview.4.20251.6 10,293 5/18/2020
5.0.0-preview.3.20214.6 46,423 4/23/2020
5.0.0-preview.2.20160.6 31,580 4/2/2020
5.0.0-preview.1.20120.5 9,189 3/16/2020
4.7.0 81,584,893 12/3/2019
4.7.0-preview3.19551.4 62,315 11/13/2019
4.7.0-preview2.19523.17 16,685 11/1/2019
4.7.0-preview1.19504.10 12,246 10/15/2019
4.6.0 31,117,041 9/23/2019
4.6.0-rc1.19456.4 60,293 9/16/2019
4.6.0-preview9.19421.4 9,779 9/4/2019
4.6.0-preview9.19416.11 638 9/4/2019
4.6.0-preview8.19405.3 62,300 8/13/2019
4.6.0-preview7.19362.9 117,178 7/23/2019
4.6.0-preview6.19303.8 116,409 6/12/2019
4.6.0-preview6.19264.9 630 9/4/2019
4.6.0-preview5.19224.8 72,264 5/6/2019
4.6.0-preview4.19212.13 2,505 4/18/2019
4.6.0-preview3.19128.7 43,213 3/6/2019
4.6.0-preview.19073.11 20,987 1/29/2019
4.6.0-preview.18571.3 16,811 12/3/2018
4.5.0 41,518,055 5/29/2018
4.5.0-rc1 47,809 5/6/2018
4.5.0-preview2-26406-04 24,168 4/10/2018
4.5.0-preview1-26216-02 53,832 2/26/2018
4.5.0-preview1-25914-04 348,538 11/15/2017
4.4.0 199,087,805 8/11/2017
4.4.0-preview2-25405-01 6,890 6/27/2017
4.4.0-preview1-25305-02 5,751 5/9/2017