This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3803|Reply: 7

[Tips] Use Roslyn to dynamically compile C# code and run it

[Copy link]
Posted on 7/6/2024 4:59:03 PM | | | |
Requirements: The project needs to generate different algorithms and logic according to the customer's needs, at this time, the code may not be written dead, and some rule engine may be required to execute the customized logic.

The .NET Compiler Platform, codenamed Roslyn, is Microsoft's set of open-source compiler and code analysis APIs for the C# and Visual Basic (VB.NET) languages.

Review:

The difference between the Roslyn MSBuild compiler
https://www.itsvse.com/thread-9596-1-1.html

.NET/C# Roslyn compiler platform
https://www.itsvse.com/thread-10740-1-1.html
First, create a new console application with the following reference:

The code is as follows:



We compile the C# code to produce a test.exe file size only3KB。 The resulting file test.exe cannot be run as a Windows executable, and the error is reported as follows:

Unhandled Exception: System.IO.FileNotFoundException: Failed to load a file or assembly "System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" or one of its dependencies. The system cannot find the specified file.
So, execute the console application with the following commands:

After execution, the error is reported as follows:

A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'D:\Project\ConsoleApp1\ConsoleApp1\bin\Debug\net8.0\'.
Failed to run as a self-contained app.
  - The application was run as a self-contained app because 'D:\Project\ConsoleApp1\ConsoleApp1\bin\Debug\net8.0\test.runtimeconfig.json' was not found.
  - If this should be a framework-dependent app, add the 'D:\Project\ConsoleApp1\ConsoleApp1\bin\Debug\net8.0\test.runtimeconfig.json' file and specify the appropriate framework.
Roughly speaking, you need a *.runtimeconfig.json file, which contains runtime information and the version of .NET Core used, reference:The hyperlink login is visible.

So, create a new test.runtimeconfig.json file with the following configuration:

Re-run the command and find that it can be executed normally, as shown in the following figure:



Reference:

The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.





Previous:.NET/C# converts PDFs to images based on PDFtoImage
Next:ERP Kingdee Cloud Starry Sky (1) Simple understanding
 Landlord| Posted on 7/6/2024 4:59:46 PM |
.NET/C# implements DLL plugin hot updates based on AssemblyLoadContext
https://www.itsvse.com/thread-10783-1-1.html
 Landlord| Posted on 7/24/2024 5:25:55 PM |
 Landlord| Posted on 12/3/2024 10:14:58 AM |
C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0 和 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.11\ 区别



Reference assemblies (type information and metadata, not including specific implementations!) ):C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.11\ref\net8.0
Runtime library (specific implementation): C:\Program Files\dotnet\shared\Microsoft.NETCore.App\8.0.11\



 Landlord| Posted on 12/3/2024 10:16:03 AM |
Reference assemblies and documentation




 Landlord| Posted on 12/10/2024 5:46:00 PM |
At the same time, you can generate a symbol file (.pdb)
 Landlord| Posted on 12/10/2024 5:49:22 PM |
Configure release mode
 Landlord| Posted on 6/30/2025 1:40:49 PM |
Little slag Posted on 2024-7-24 17:25
https://github.com/roslynpad/roslynpad/releases

LINQPad:https://www.linqpad.net/
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com