RSCG_Static by Andrei Ignat
Nuget / site data
Details
Info
Name: RSCG_Static
This package make you an interface and record from static properties / methods
Author: Andrei Ignat
NuGet: https://www.nuget.org/packages/RSCG_Static/
You can find more details at https://github.com/ignatandrei/RSCG_Static
Original Readme
RSCG_Static
Roslyn Source Code Generator - transform static classes into instances and interfaces
More, there is a MakeNew static method created to can have DI.
Just put a function like this ( example for System.DateTime)
public Type GenerateInterfaceFromDate()=>typeof(DateTime);
and the properties of the classes will be generated into interfaces and you can write:
//for DI, register
//ISystem_DateTime with transient for new clsSystem_DateTime()
Console.WriteLine("Hello World!");
ISystem_DateTime dateStatic = recSystem_DateTime.MakeNew();//static
ISystem_DateTime dateVar = new clsSystem_DateTime(); //variable = real
Console.WriteLine(dateStatic.Now.Second);
Console.WriteLine(dateVar.Now.Second);
await Task.Delay(10 * 1000);
Console.WriteLine(dateStatic.Now.Second);
Console.WriteLine(dateVar.Now.Second);
More Roslyn Source Code Generators
You can find more RSCG with examples at Roslyn Source Code Generators
About
Generate interfaces and classes from static classes
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- StaticToInterface.cs
This is the CSharp Project that references RSCG_Static
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RSCG_Static" Version="2023.5.19.2037" />
</ItemGroup>
</Project>
This is the use of RSCG_Static in Program.cs
using RSCG_StaticDemo;
//for DI, register
//ISystem_DateTime with transient for new clsSystem_DateTime()
Console.WriteLine("Hello World!");
ISystem_DateTime dateStatic = recSystem_DateTime.MakeNew();//static
ISystem_DateTime dateVar = new clsSystem_DateTime(); //variable = real
Console.WriteLine(dateStatic.Now.Second);
Console.WriteLine(dateVar.Now.Second);
await Task.Delay(10 * 1000);
Console.WriteLine(dateStatic.Now.Second);
Console.WriteLine(dateVar.Now.Second);
This is the use of RSCG_Static in StaticToInterface.cs
namespace RSCG_StaticDemo;
public partial class StaticToInterface
{
public Type GenerateInterfaceFromDate() => typeof(DateTime);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- GenerateInterfaceFromDate.cs
#nullable enable
namespace RSCG_StaticDemo {
public interface ISystem_DateTime {
public System.DateTime Now {get;}
public System.DateTime Today {get;}
public System.DateTime UtcNow {get;}
}// interface
//now the partial class
public record recSystem_DateTime (System.DateTime Now,System.DateTime Today,System.DateTime UtcNow) : ISystem_DateTime
{
public static recSystem_DateTime MakeNew() {
return new recSystem_DateTime(System.DateTime.Now,System.DateTime.Today,System.DateTime.UtcNow);
} //end makenew
} //end record
public class clsSystem_DateTime : ISystem_DateTime
{
public System.DateTime Now {get { return System.DateTime.Now; } }
public System.DateTime Today {get { return System.DateTime.Today; } }
public System.DateTime UtcNow {get { return System.DateTime.UtcNow; } }
} //end class
} // namespace
#nullable disable
Usefull
Download Example (.NET C# )
Share RSCG_Static
https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_Static