|
| 1 | +## About |
| 2 | + |
| 3 | +<!-- A description of the package and where one can find more documentation --> |
| 4 | + |
| 5 | +Provides functionality for dynamically generating and compiling source code using the Code Document Object Model (CodeDOM). |
| 6 | + |
| 7 | +It allows developers to represent code in a language-agnostic format and then generate code in multiple languages, such as C# and VB.NET. |
| 8 | +The primary use cases include creating dynamic code generation tools, runtime code generation, and facilitating code analysis or transformation. |
| 9 | + |
| 10 | +For a new modern development consider using the [.NET Compiler Platform SDK](https://learn.microsoft.com/dotnet/csharp/roslyn-sdk/), in particular [Roslyn source generators](https://learn.microsoft.com/dotnet/csharp/roslyn-sdk/source-generators-overview#get-started-with-source-generators). |
| 11 | + |
| 12 | +## Key Features |
| 13 | + |
| 14 | +<!-- The key features of this package --> |
| 15 | + |
| 16 | +* Write code using a common object model that can be translated into multiple programming languages. |
| 17 | +* Generate and compile code at runtime based on the CodeDOM. |
| 18 | + |
| 19 | +## How to Use |
| 20 | + |
| 21 | +<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package --> |
| 22 | + |
| 23 | +Generating and compiling C# code: |
| 24 | + |
| 25 | +```csharp |
| 26 | +using System.CodeDom; |
| 27 | +using System.CodeDom.Compiler; |
| 28 | +using Microsoft.CSharp; |
| 29 | + |
| 30 | +// Create a new CodeCompileUnit to hold the code |
| 31 | +var compileUnit = new CodeCompileUnit(); |
| 32 | + |
| 33 | +// Create a namespace |
| 34 | +var codeNamespace = new CodeNamespace("MyNamespace"); |
| 35 | +compileUnit.Namespaces.Add(codeNamespace); |
| 36 | + |
| 37 | +// Create a class |
| 38 | +var classDeclaration = new CodeTypeDeclaration("MyClass") |
| 39 | +{ |
| 40 | + IsClass = true |
| 41 | +}; |
| 42 | +codeNamespace.Types.Add(classDeclaration); |
| 43 | + |
| 44 | +// Add a simple method to the class |
| 45 | +var method = new CodeMemberMethod |
| 46 | +{ |
| 47 | + Name = "HelloWorld", |
| 48 | + ReturnType = new CodeTypeReference(typeof(void)), |
| 49 | +}; |
| 50 | +classDeclaration.Members.Add(method); |
| 51 | + |
| 52 | +var methodInvocation = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("Console"), |
| 53 | + "WriteLine", |
| 54 | + new CodePrimitiveExpression("Hello, World!")); |
| 55 | +method.Statements.Add(methodInvocation); |
| 56 | + |
| 57 | +// Generate C# code from the CodeDOM structure |
| 58 | +CodeDomProvider provider = new CSharpCodeProvider(); |
| 59 | + |
| 60 | +using (var writer = new StringWriter()) |
| 61 | +{ |
| 62 | + var codeGenereationOptions = new CodeGeneratorOptions() |
| 63 | + { |
| 64 | + BlankLinesBetweenMembers = false, |
| 65 | + IndentString = " ", |
| 66 | + }; |
| 67 | + |
| 68 | + provider.GenerateCodeFromCompileUnit(compileUnit, writer, codeGenereationOptions); |
| 69 | + Console.WriteLine(writer.GetStringBuilder().ToString()); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +This example generates: |
| 74 | + |
| 75 | +```csharp |
| 76 | +//------------------------------------------------------------------------------ |
| 77 | +// <auto-generated> |
| 78 | +// This code was generated by a tool. |
| 79 | +// |
| 80 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 81 | +// the code is regenerated. |
| 82 | +// </auto-generated> |
| 83 | +//------------------------------------------------------------------------------ |
| 84 | +
|
| 85 | +namespace MyNamespace { |
| 86 | + |
| 87 | + public class MyClass { |
| 88 | + private void HelloWorld() { |
| 89 | + Console.WriteLine("Hello, World!"); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Main Types |
| 96 | + |
| 97 | +<!-- The main types provided in this library --> |
| 98 | + |
| 99 | +The main types provided by this library are: |
| 100 | + |
| 101 | +* `System.CodeDom.CodeObject` |
| 102 | +* `System.CodeDom.CodeCompileUnit` |
| 103 | +* `System.CodeDom.CodeNamespace` |
| 104 | +* `System.CodeDom.CodeTypeDeclaration` |
| 105 | +* `System.CodeDom.CodeMemberMethod` |
| 106 | +* `System.CodeDom.CodeTypeReference` |
| 107 | +* `System.CodeDom.CodeMethodInvokeExpression` |
| 108 | +* `System.CodeDom.CodeTypeReferenceExpression` |
| 109 | +* `System.CodeDom.CodePrimitiveExpression` |
| 110 | +* `System.CodeDom.Compiler.CodeDomProvider` |
| 111 | +* `System.CodeDom.Compiler.CodeGeneratorOptions` |
| 112 | +* `Microsoft.CSharp.CSharpCodeProvider` |
| 113 | +* `Microsoft.VisualBasic.VBCodeProvider` |
| 114 | + |
| 115 | +## Additional Documentation |
| 116 | + |
| 117 | +<!-- Links to further documentation. Remove conceptual documentation if not available for the library. --> |
| 118 | + |
| 119 | +* [API documentation](https://learn.microsoft.com/dotnet/api/system.codedom) |
| 120 | +* [Compile and generate dynamic source code](https://learn.microsoft.com/dotnet/framework/reflection-and-codedom/dynamic-source-code-generation-and-compilation) |
| 121 | + |
| 122 | +## Feedback & Contributing |
| 123 | + |
| 124 | +<!-- How to provide feedback on this package and contribute to it --> |
| 125 | + |
| 126 | +System.CodeDom is released as open source under the [MIT license](https://licenses.nuget.org/MIT). |
| 127 | +Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime). |
0 commit comments