METHOD OF EXECUTING DYNAMIC CALLS IN .NET USING SOURCE CODE GENERATION

Authors

DOI:

https://doi.org/10.35546/kntu2078-4481.2025.2.2.13

Keywords:

metaprogramming, .NET, reflection, code generation, Source Generators

Abstract

The article discusses the development of a method for executing dynamic calls in .NET by generating code at the compilation. The traditional mechanism for making dynamic calls in .NET, based on the use of reflection, is analyzed. It is determined that the key disadvantage of such an approach is its performance, which necessitates the search for an alternative solution for making dynamic calls. A review of recent publications on the topic of optimizing dynamic calls in .NET is conducted. As a result of the analysis, it is determined that a number of existing solutions describe only the process of making a call, but do not consider the storage of generated functions and a structure that would allow for their reuse. A solution based on the generation of metadata at the compilation stage and the association of such metadata with corresponding static calls is considered. A solution is proposed that is also based on the generation of the metadata during compilation and their association with static calls. The advantage of the proposed solution over the existing one is the ability to make dynamic calls for objects cast to one of the basic types, which expands the list of scenarios for the proposed method’s application. This was achieved by using a metadata structure that is similar to the reflection. Metadata generation is implemented with Incremental Source Generators, which allows for efficient code generation. The method involves using a combination of two code generators, one of which is responsible for generating the metadata itself, and the other for generating code that combines such metadata into a single structure. To determine data types for which generation is required, it is proposed to use both metadata attributes and special methods that accept the data type as an argument. This allowed for the proposed method to be used for data types defined in the context of the current syntax tree and data types belonging to external libraries. The performance of the developed method was tested using the Benchmark.NET library, as a result of which it was found that the proposed method has better performance compared to reflection.

References

Bock J., Hazzard K. Metaprogramming In.NET. Manning Publications Co. LLC, 2012.

Ingebrigtsen E. Metaprogramming in C#: Automate Your.NET Development and Simplify Overcomplicated Code. Packt Publishing, Limited, 2023.

Beaumont A., Bakhtiari Bastaki B. An Investigation into the Prevalence of Reflection Techniques in Distributed Microsoft.Net NuGet Artefacts. ICSCA 2022: 2022 11th International Conference on Software and Computer Applications, м. Melaka Malaysia. New York, NY, USA, 2022. URL: https://doi.org/10.1145/3524304.3524329

Warren M. Why is reflection slow?. URL: https://mattwarren.org/2016/12/14/Why-is-Reflection-slow/ (дата звернення: 17.05.2025).

Haytam Z. C# Expression Trees: Property Getters. URL: https://blog.zhaytam.com/2020/11/17/expression-trees-property-getter/ (дата звернення: 17.05.2025).

Ricardo Peres. Using Generated Methods Instead of Reflection. URL: https://weblogs.asp.net/ricardoperes/using- generated-methods-instead-of-reflection (дата звернення: 17.05.2025).

Silin S. Reflection via source generators. URL: https://dev.to/byme8/aot-reflection-4ijb (дата звернення: 17.05.2025).

Microsoft. Introducing C# Source Generators. URL: https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/ (дата звернення: 17.05.2025).

Reflekt: a Library for Compile-Time Reflection in Kotlin / A. Birillo та ін. 2022 IEEE/ACM 44th International Conference on Software Engineering: Software Engineering in Practice (ICSE-SEIP), м. Pittsburgh, PA, USA, 22–24 трав. 2022 р. 2022. URL: https://doi.org/10.1109/icse-seip55303.2022.9793932

Gerr P. Incremental Roslyn Source Generators In.NET 6. URL: https://www.thinktecture.com/net/roslyn-source-generators-introduction/ (дата звернення: 17.05.2025).

BenchmarkDotNet. URL: https://benchmarkdotnet.org/index.html (дата звернення: 17.05.2025).

Published

2025-06-05