RefleCS 0.2.0
dotnet add package RefleCS --version 0.2.0
NuGet\Install-Package RefleCS -Version 0.2.0
<PackageReference Include="RefleCS" Version="0.2.0" />
paket add RefleCS --version 0.2.0
#r "nuget: RefleCS, 0.2.0"
// Install RefleCS as a Cake Addin #addin nuget:?package=RefleCS&version=0.2.0 // Install RefleCS as a Cake Tool #tool nuget:?package=RefleCS&version=0.2.0
RefleCS
Description
RefleCS is a library that sits in front of the Roslyn C# parser and makes it easier to create new or edit existing C# files that are outside of an Assembly context.
Though basic work is done, RefleCS is still missing some CS language features. If you encounter any, please file an issue.
Examples
Create new file
You can create a new file by creating an instance of CsFile
and saving it via the CsFileHandler
var file = new CsFile(
new List<Using>
{
new("System")
},
new Namespace(
"MyApp",
new List<Class>
{
new(
new List<ClassModifier>
{
ClassModifier.Public,
ClassModifier.Sealed
},
"App",
new List<Constructor>(),
new List<Property>(),
new List<Method>
{
new(
new List<Comment>(),
new List<MethodModifier>(),
"void",
"CheckIfTrue",
new List<Parameter>
{
new(
"bool",
"bl")
},
new List<Statement>
{
new("return bl;")
})
},
new List<BaseType>())
},
Enumerable.Empty<Record>()));
new CsFileHandler().SaveOrReplace(file, @"C:/Temp/mycsfile.cs");
This will create the following file content:
using System;
namespace MyApp;
public sealed class App
{
void CheckIfTrue(bool bl)
{
return bl;
}
}
However, after creation of a CsFile
, it's still possible to edit classes, methods, etc. For example by adding a new parameterless constructor:
file.Nmsp.Classes.First()
.AddConstructor(
new Constructor(
new List<ConstructorModifier>() { ConstructorModifier.Public },
"App",
new List<Parameter>(),
null,
new List<Statement>() { new("Console.WriteLine(\"Ctor called!\");") }
));
Edit an existing file
You can create a CsFile
instance from an existing file in the file system
CsFile file = new CsFileHandler().FromPath(@"C:/Temp/mycsfile.cs");
or from C# code already loaded as a string
var csCode = @"using System;
namespace MyApp;
public class App {}";
CsFile file = new CsFileHandler().FromContent(csCode);
Then you can edit the CsFile
and save it.
file.Nmsp.Classes.First()
.AddConstructor(
new Constructor(
new List<ConstructorModifier>() { ConstructorModifier.Public },
"App",
new List<Parameter>(),
null,
new List<Statement>() { new("Console.WriteLine(\"Ctor called!\");") }
));
new CsFileHandler().SaveOrReplace(file, @"C:/Temp/mycsfile.cs");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.2.0 | 128 | 8/17/2024 |
0.2.0-alpha.6 | 182 | 11/11/2023 |
0.2.0-alpha.5 | 83 | 8/6/2023 |
0.2.0-alpha.4 | 99 | 1/21/2023 |
0.2.0-alpha.3 | 113 | 10/15/2022 |
0.1.0 | 415 | 9/11/2022 |
0.1.0-alpha.3 | 98 | 8/24/2022 |