RefleCS 0.2.0-alpha.3

This is a prerelease version of RefleCS.
There is a newer version of this package available.
See the version list below for details.
dotnet add package RefleCS --version 0.2.0-alpha.3                
NuGet\Install-Package RefleCS -Version 0.2.0-alpha.3                
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="RefleCS" Version="0.2.0-alpha.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RefleCS --version 0.2.0-alpha.3                
#r "nuget: RefleCS, 0.2.0-alpha.3"                
#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 RefleCS as a Cake Addin
#addin nuget:?package=RefleCS&version=0.2.0-alpha.3&prerelease

// Install RefleCS as a Cake Tool
#tool nuget:?package=RefleCS&version=0.2.0-alpha.3&prerelease                

RefleCS

Nuget

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>())
        }));

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>(),
            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>(),
            new List<Statement>() { new("Console.WriteLine(\"Ctor called!\");") }
        ));

new CsFileHandler().SaveOrReplace(file, @"C:/Temp/mycsfile.cs");
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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