-
Jun 22nd, 2021, 03:25 PM
#1
Thread Starter
Lively Member
Creating VB.NET Records: The magic of Roslyn and source generators!
I created a Record Generator to allow you to use Records in your VB.NET prpjects.
These are three possible variations of the record syntax:
Code:
Public Record NameValue(Name ="", Value = 0.0)
Public ReadOnly Structure ROStruct(X$, Y%, Z@)
Friend ReadOnly Class ROClass(A As Integer, B As Integer)
To use the Record Generator:
- Add the NuGet package to your project.
PM> Install-Package Visual-Basic-Record-Generator - Add one or more text files to your project, and change there extension to .rec.
- Right-click the .rec file in the Solution Explorer and click Properties, then from the 'Build Action' dropdown list choose VB analyzer additional file and save the changes.
- Write the one or more record syntax in each .rec file and save the changes. The generator will generate the record classes/structures immediately for you, and you can use them in your code as a part of your project.
For more info on record syntax, see the `Read Me` on the source code repo on github:
-
Jun 22nd, 2021, 03:29 PM
#2
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
Moderator actions - Moved thread to Visual Basic .NET codebank, replaced markdown with BB tags, and removed the obnoxiously large font size.
-
Jun 22nd, 2021, 04:24 PM
#3
Thread Starter
Lively Member
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
Originally Posted by dday9
Moderator actions - Moved thread to Visual Basic .NET codebank, replaced markdown with BB tags, and removed the obnoxiously large font size.
That's OK. Thanks.
-
Aug 2nd, 2021, 11:46 AM
#4
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
Man, I really need to learn how to use stuff like this. There is so much potential with these compilation toolset.
-
Aug 2nd, 2021, 01:51 PM
#5
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
Doesn't PetaPoco do something similar using T4 templates or is this something completely different?
-
Aug 3rd, 2021, 05:57 AM
#6
Thread Starter
Lively Member
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
Originally Posted by Niya
Man, I really need to learn how to use stuff like this. There is so much potential with these compilation toolset.
You can start here:
https://github.com/DualBrain/Samples...urceGenerators
-
Aug 3rd, 2021, 06:02 AM
#7
Thread Starter
Lively Member
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
Originally Posted by dday9
Doesn't PetaPoco do something similar using T4 templates or is this something completely different?
Source generators can take actions based on the vb source code, and make use of the syntax tree and the compilation/semantics of the current context. This is way advanced than T4 which can only generate code based on a text template. See the samples I posted in the above reply.
-
Aug 3rd, 2021, 06:15 AM
#8
Thread Starter
Lively Member
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
By the way, this my first try to generate vb records:
https://github.com/VBAndCs/VB-Record-Generator
It just uses a text template. It is just an utility and you have to copy the generated record and add it to your project manually. The source generators version is more advances, and it is easy to modify the record definition as it is a part of the project. It can infer param types from their default values, and solved the missing optional param confusion by using Nullable/Optional Structures. In the old version, I just added ? for all params and you must fix the errors when the param type is a reference type because I can't know that without information about your project and its references. Source generators makes this possible.
-
Dec 17th, 2022, 08:12 PM
#9
Thread Starter
Lively Member
Re: Creating VB.NET Records: The magic of Roslyn and source generators!
RecGen is updated to v2.2, to generate the GetHashCode method which is necessary to use the record as a key in dictionaries.
To add the RecGen NuGet package to your project, use:
PM> Install-Package RecGen
Or:
PM> Install-Package Visual-Basic-Record-Generator
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|