Skip to content

Commit c381595

Browse files
eNeRGy164buyaa-n
andauthored
Provide System.CodeDom package readme (#107372)
* Provide System.CodeDom package readme * Add remark about Roslyn/.NET Compiler SDK * Update src/libraries/System.CodeDom/src/PACKAGE.md --------- Co-authored-by: Buyaa Namnan <buyankhishig.namnan@microsoft.com>
1 parent c4cc794 commit c381595

File tree

2 files changed

+128
-9
lines changed

2 files changed

+128
-9
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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).

src/libraries/System.CodeDom/src/System.CodeDom.csproj

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@
66
<IsTrimmable>false</IsTrimmable>
77
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
88
<IsPackable>true</IsPackable>
9-
<PackageDescription>Provides types that can be used to model the structure of a source code document and to output source code for that model in a supported language.
10-
11-
Commonly Used Types:
12-
System.CodeDom.CodeObject
13-
System.CodeDom.Compiler.CodeDomProvider
14-
Microsoft.CSharp.CSharpCodeProvider
15-
Microsoft.VisualBasic.VBCodeProvider</PackageDescription>
9+
<PackageDescription>Provides types that can be used to model the structure of a source code document and to output source code for that model in C# or Visual Basic.</PackageDescription>
1610

1711
<!-- TODO https://github.com/dotnet/runtime/issues/90400: Annotate for nullable reference types -->
1812
<Nullable>disable</Nullable>
1913
<NoWarn>$(NoWarn);nullable</NoWarn>
20-
<!-- TODO: Add package README file: https://github.com/dotnet/runtime/issues/99358 -->
21-
<EnableDefaultPackageReadmeFile>false</EnableDefaultPackageReadmeFile>
2214
</PropertyGroup>
2315

2416
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->

0 commit comments

Comments
 (0)