Artykuł przedstawia podejście do implementacji funkcjonalności cech w języku programowania C# poprzez wykorzystanie Roslyn Source Generators, narzędzia do metaprogramowania w czasie kompilacji wprowadzonego w .NET5. Traity, które są komponowalnymi jednostkami drobnoziarnistego ponownego użycia, mogą przynieść korzyści językowi C# poprzez wprowadzenie mechanizmu ponownego użycia zachowania w kontekście pojedynczego dziedziczenia. W artykule opisano projekt i implementację generatora źródłowego, który umożliwia dodawanie adnotacji do klas za pomocą atrybutów metadanych, automatycznie wstrzykując odpowiednie definicje elementów członkowskich w czasie kompilacji. Aby osiągnąć lepszą wydajność generowania kodu w czasie projektowania, zastosowano przyrostowe generatory źródeł. Szczegółowo opisano skład członków cech, koncentrując się na rozwiązywaniu konfliktów nazw i bezpieczeństwie typów poprzez wykorzystanie interfejsów i mechanizmu zastępowania. Omówiono i zaimplementowano również definiowanie niepublicznych członków w kontekście cechy, co pozwala na lepsze ponowne wykorzystanie kodu. Artykuł podsumowuje, że funkcje na poziomie języka, takie jak cechy, można zaimplementować w języku C# za pomocą Roslyn Source Generators, demonstrując w ten sposób potencjał takiego narzędzia i ogólnie metaprogramowania.
Bibliografia
[1] Albalooshi F., Mahmood A.: A Comparative Study on the Effect of Multiple Inheritance Mechanism in Java, C++, and Python on Complexity and Reusability of Code. Int. J. Adv. Comput. Sci. Appl. 8(6), 2017, 109–116 [https://doi.org/10.14569/ijacsa.2017.080614].
[2] Arora H., Servetto M., Oliveira B. C. D. S.: Separating Use and Reuse to Improve Both. The Art, Science, and Engineering of Programming 3(3), 2019 [https://doi.org/10.22152/programming-journal.org/2019/3/12].
[3] AutoInterface, 2025 [https://github.com/beakona/AutoInterface].
[4] Bergel A. et al.: Stateful Traits. De Meuter W. (eds): Advances in Smalltalk. ISC 2006. Lecture Notes in Computer Science 4406. Springer, Berlin, Heidelberg 2007 [https://doi.org/10.1007/978-3-540-71836-9_4]
[5] Black A. P., Schärli N., Ducasse S.: Applying Traits to the Smalltalk Collection Classes. ACM SIGPLAN Not. 38(11), 2003, 47–64 [https://doi.org/10.1145/949343.949311].
[6] Cassou D., Ducasse S., Wuyts R.: Traits at work: The design of a new trait-based stream library. Comput. Lang., Syst. & Struct. 35(1), 2009, 2–20 [https://doi.org/10.1016/j.cl.2008.05.004].
[7] Ducasse S. et al.: Traits: A mechanism for fine-grained reuse. ACM Trans. Program. Lang. Syst. 28(2), 2006, 331–388 [https://doi.org/10.1145/1119479.1119483]
[8] Ducournau R., Morandat F., Privat J.: Empirical assessment of object-oriented implementations with multiple inheritance and static typing. ACM SIGPLAN Not. 44(10), 2009, 41–60 [https://doi.org/10.1145/1639949.1640093].
[9] Explicit Interface Implementation – C#, 2022 [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation].
[10] Gerr P.: Incremental Roslyn Source Generators In .NET 6: Code Sharing Of The Future – Part 1. 2022 [https://www.thinktecture.com/net/roslyn-source-generators-introduction/].
[11] Hossain M.: Understanding Traits in PHP: A Guide with Examples, 2024 [https://mohasin-dev.medium.com/understanding-traits-in-php-a-guide-with-examples-84f14f1b7b43].
[12] Immutype, 2025 [https://github.com/DevTeam/Immutype].
[13] Introducing C# Source Generators, 2025 [https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/].
[14] Lock A.: Understanding C# 8 default interface methods. 2024 [https://andrewlock.net/understanding-default-interface-methods/].
[15] Mr Prime: Mixins in Ruby, 2024 [https://medium.com/@primedruk312/mixins-in-ruby-09e58b226e50].
[16] Muniyandi V.: Harnessing Roslyn for Advanced Code Analysis and Optimization in Cloud- Based .NET Applications on Microsoft Azure. Int. J. Commun. Netw. Secur. 14(3), 2022, 979–990 [https://ijcnis.org/index.php/ijcnis/article/view/8051/2231].
[17] Nichols C., Klabnik S.: Rust Programming Language, 2nd Edition. No Starch Press, Incorporated, 2023.
[18] Partial Classes and Members – C#, 2025 [https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods].
[19] Schärli N. et al.: Traits: Composable Units of Behaviour. In: ECOOP 2003 – Object-Oriented Programming. Springer Berlin Heidelberg, 2003, 248–274 [https://doi.org/10.1007/978-3-540-45070-2_12].
[20] Steyaert P. et al.: Nested Mixin-Methods in Agora. Nierstrasz O. M. (eds): ECOOP’ 93 — Object-Oriented Programming. Springer Berlin Heidelberg, 1993 [https://doi.org/10.1007/3-540-47910-4_12].
[21] StrongInject, 2025 [https://github.com/YairHalberstadt/stronginject].
[22] Tesone P. et al.: A new modular implementation for stateful traits. Sci. Comput. Program. 195, 2020, 102470 [https://doi.org/10.1016/j.scico.2020.102470].
[23] Traits: Defining Shared Behavior, 2025 [https://doc.rust-lang.org/book/ch10-02-traits.html].
[24] Wampler D., Payne A.: Programming Scala. O'Reilly Media, Incorporated, 2014.
[25] Yaqoob E.: Mixins in dart, 2023 [https://medium.com/@emanyaqoob/mixins-in-dart-aed7e89de15d].