Skip to content

C# Source generator helping with certain tasks concerning member access, like generating public properties for private variables

License

Notifications You must be signed in to change notification settings

Thomas-Mielke-Software/MemberAccess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MemberAccess

C# Source generator helping with certain tasks concerning member access, like generating public properties for private variables.

Currently, there is only one generator [GeneratePropertiesForAllPrivateVariables] included. In the future I plan other generators, for example to to provide support for CommunityToolkit.Mvvm attributes on class level.

Intention and Usage

I recently had a model class with a lot of variable members and was unnerved by having to type public all along:

public class Demo
{
    public int id;
    public string name;
}

With this source generator, you can write a class without public keywords like this:

[GeneratePropertiesForAllPrivateVariables]
public partial class Demo
{
    int id;
    string name;
}

This will generate the following shadow class with accessors:

public partial class Demo
{
    public int Id
    {
        get => id;
        set => id = value;
    }

    public string Name
    {
        get => name;
        set => name = value;
    }
}

Now you can access the members by their upper-case properties (Note: also C++style m_ and _ prefixes are translated correctly):

    var demoInstance = new Demo();
    demoInstance.Id = 42;
    demoInstance.Name = "Foo";

You should make the [GeneratePropertiesForAllPrivateVariables] attribute known to your project somewhere, so the code analyzer doesn't put a red zig-zag line under it:

[AttributeUsage(AttributeTargets.Class/* ToDo: | System.AttributeTargets.Struct */, AllowMultiple = false, Inherited = false)]
public sealed class GeneratePropertiesForAllPrivateVariablesAttribute : Attribute
{
}

You also need to reference the source generator as "Analyzer" from the consuming project:

After this (and possibly a Visual Studio restart), you will be able to check the generated code in Solution Explorer under References -> Analyse -> MemberAccess.

Support for snake_case to CamelCase transformation

Models from APIs often come in the snake_case json form. If you set the attribute argument snakeCase2CamelCase to true, a snake_case to CamelCase transformation for each private variable member takes place:

[GeneratePropertiesForAllPrivateVariables(true)]
public partial class Demo
{
    string first_name;
    string last_name;
}

This way you have an easier time adapting the server-side models to C# conformimg CamelCase.

Limitations

Currently only public non-static classes are supported. It's a bit hacky here and there, but should do the job in 99% of all cases. Consider it a v0.1. Look for 'ToDo', if you want to know more.

If you add some useful features or fix a bug: pull requests welcome!

License

MIT

About

C# Source generator helping with certain tasks concerning member access, like generating public properties for private variables

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages