Giter Site home page Giter Site logo

rscg_utils's Introduction

RSCG_Utils

Roslyn Source Code Generators Utils

pack to nuget

pack to nuget

Usage

Additional Files

Allow you to see additional files directly as C# const. For this, please add some .gen. files to the project.

In your csproj

<ItemGroup>
 	  <PackageReference Include="rscgutils" Version="2024.2000.2000" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
	<AdditionalFiles Include="Second.gen.txt" />
	<AdditionalFiles Include="first.gen.txt" />
	<AdditionalFiles Include="test\Afirst.gen.txt" />
	<AdditionalFiles Include="sql/**/*" />
</ItemGroup>

In the code

//see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string
string x= MyAdditionalFiles.Second_gen_txt;

To debug, you can add into the .csproj

<PropertyGroup>
	<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
	<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

More details at http://msprogrammer.serviciipeweb.ro/2023/05/08/file-to-csharp-const/

Json2Class

If you have an additional json file, you can have a file from this

<ItemGroup>
	<AdditionalFiles Include="my.gen.json" />
</ItemGroup>

And you can have from the code

var json = System.Text.Json.JsonSerializer
    .Deserialize<GeneratedJson.my_gen_json>(MyAdditionalFiles.my_gen_json);

ArgumentNullException.ThrowIfNull( json );
Console.WriteLine( ":hosts"+json.AllowedHosts );

Memoization of function returns

Put _MemoPure and the return of the function will be memo-ized

public long Test_MemoPure()
{
    Console.WriteLine("calculating type");
    return this.GetType().ToString().GetHashCode();
}
public async Task<long> fib(long nr)
{
    await Task.Delay(1000);
    //Console.WriteLine("calculated value for " + nr);
    if (nr <= 1) return 1;
    if (nr == 2) return 2;
    return await fib(nr - 1) + await fib(nr - 1);
}

public async Task<long> fibonacci_MemoPure(long nr)
{
    if (nr <= 1) return 1;
    if (nr == 2) return 2;
    await Task.Delay(1000);
    return await fibonacci(nr - 1) + await fibonacci(nr - 1);

}

And call

fibTest f = new();
Console.WriteLine("first time :" + f.Test());
Console.WriteLine("second time :" + f.Test());

Console.WriteLine(DateTime.Now.ToString("mm_ss"));
Console.WriteLine("no memo :"+await f.fib(5));
Console.WriteLine(DateTime.Now.ToString("mm_ss"));
Console.WriteLine("memo :" + await f.fibonacci(5));
Console.WriteLine(DateTime.Now.ToString("mm_ss"));
Console.WriteLine("FAST memo :" + await f.fibonacci(5));
Console.WriteLine(DateTime.Now.ToString("mm_ss"));

More Roslyn Source Code Generators

You can find more RSCG with examples at Roslyn Source Code Generators

rscg_utils's People

Contributors

ignatandrei avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.