Dynamic Code Framework Release - CZGL.Roslyn

2020年11月14日 50点热度 0人点赞 0条评论
内容目录

CZGL.Roslyn

Open source project location: https://github.com/whuanle/CZGL.CodeAnalysis

A dynamic code builder and compiler for C# based on Roslyn technology, developers can use this library to dynamically construct C# code and compile it conditionally.

  • Runtime dynamic code construction;
  • Providing new module functionality for programs at runtime, expanding capabilities;
  • Designing a function platform that executes code snippets;
  • Syntax analysis prompts, compile-time error syntax warnings, precise to the line of code;
  • Complete code standards to avoid dynamic code syntax errors;
  • Simplified, straightforward API with comprehensive testing;
  • ... ...

Animation Demonstration

This demonstration uses Blazor for testing, and the code can be found in the source code directory examples/BlazorExample.

Blazor Running CZGL.Roslyn

The Blazor demonstration compiles on a namespace basis; by customizing the backend, you can run it with just blocks of code, without needing function headers. More features await your exploration!

Rich Documentation and Examples

CZGL.Roslyn has extensive documentation and API interface explanations, making it easy for you to use!

Documentation URL: https://1586052146.gitbook.io/codeanalysis

API URL: https://czgl-roslyn.github.io/czgl-roslyn

Rich Documentation

API Documentation

Version Update Plan

  • [ ] 0.1.0 α version (under development, current version)

    Features: A primary version with complete functionalities.

  • [ ] 0.2.0 β (beta) version

    Features: Elimination of severe bugs and defects, completion of substantial unit tests, significantly improving code reliability; able to go live on Nuget for user trials; collection of user demand for program improvements;

  • [ ] 0.3.0 γ version

This version is quite mature, with updates based on user feedback and requirements after using the β version; it begins to focus on performance, conducting performance testing and implementing measures for improvement.

What Can Be Done

The biggest feature is dynamic code construction and dynamic compilation, so we can discuss it in two parts.

Dynamic Code Generation

  • Restoring entities from database tables, automatically generating class model files; (EFCore has this feature)

  • Internal toolset defined by the company, generating code with one-click using defined templates!

  • Quickly generating code exports based on established logical functions!

  • Writing AOP framework, implementing dynamic proxies;

  • Code syntax checking;

  • Generating scheduled code, managing which parts of the code can be executed in the background;

  • Workflow programs constructed once through code generation, embedding workflows directly into the assembly, enhancing performance!

  • Quickly generating code from various data sources like tables;

  • Custom UI generators that generate code by dragging controls;

    ... ...

Dynamic Compilation

  • Self-developed cloud function platform, users can upload code for direct execution;

  • Company backend scheduled tasks, customizing execution assembly by assembly;

  • Writing AOP framework, dynamically compiling assemblies;

  • Single file (.cs) compilation, no SDK or IDE needed! Compile outside the development environment!

    ... ...

Relationship with Natasha

Natasha is an excellent dynamic code framework, led by Dashuai, which has formed a complete functionality, strong performance, and compatibility with .NET FX 4.6~.NET Core ↑, passing numerous performance tests and high-performance code optimizations.

It can be described as incredibly powerful. Repository address: https://github.com/dotnetcore/Natasha

So why write CZGL.Roslyn?

1. To hone technical skills;

2. It does not constitute a competing product to Natasha, CZGL.Roslyn aims to be as lightweight as possible; developers can consider business size, using Natasha for large scenes; quick construction using CZGL.Roslyn; Natasha has a steeper learning curve, CZGL.Roslyn is quicker to get started.

3. Natasha has complete features and greater dependencies; CZGL.Roslyn aims to be sufficiently functional while maintaining minimal dependencies and smaller size;

4. CZGL.Roslyn is suitable for writing small tools, such as dotnet-tool, restoring entities from databases, providing quick code generation for ML.NET, etc., suitable as a widget to provide additional functionality for your program; Natasha is more suited as an independent product to build around it with more features;

In terms of performance, Natasha is outstanding; regarding feature completeness, Natasha offers a comprehensive service package... ...

Compared to Natasha, CZGL.Roslyn’s main characteristic is "lightweight," and it cannot compete with Natasha in other aspects...

What Can I Learn from This Framework

The framework features simplicity and ease of use, and you can learn a lot about reflection and Roslyn knowledge from it, and possibly even learn about EMIT (which I will use to optimize performance later).

For example, how to obtain attribute values from properties?

[Display(Name = "_Id")]
public int Id {get;set;}

How can you determine whether a Type is a class, struct, or delegate?

If you consult the documentation, you will find that Type.IsClass can identify classes and delegates;

Why do the execution results differ between JAVA and C# in the following code?

public class A
{
    public void Test
    {
        Console.WriteLine("A");
    }
}
public class B:A
{
    // Hiding the parent class Test function
    public new void Test
    {
        Console.WriteLine("B");
    }
}

A a = new B(); a.Test();

Currently, CZGL.Roslyn is in its initial version, and the code is in a very simple state, with straightforward logic, so if you intend to learn about reflection, dynamic code techniques, etc., in C#, and want to understand how the information is retrieved through reflection after code generation and how the source code is compiled, CZGL.Roslyn is your best choice.

痴者工良

高级程序员劝退师

文章评论