Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ufcpp/MemberAccessGenerator
https://github.com/ufcpp/MemberAccessGenerator
csharp-sourcegenerator
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/ufcpp/MemberAccessGenerator
- Owner: ufcpp
- License: mit
- Created: 2020-07-08T11:01:02.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-10T13:26:27.000Z (over 4 years ago)
- Last Synced: 2024-08-01T22:44:04.922Z (4 months ago)
- Topics: csharp-sourcegenerator
- Language: C#
- Homepage:
- Size: 13.7 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - MemberAccessGenerator
- csharp-source-generators - MemberAccessGenerator - ![stars](https://img.shields.io/github/stars/ufcpp/MemberAccessGenerator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/ufcpp/MemberAccessGenerator?style=flat-square&cacheSeconds=86400) generates `GetMember(int)` and/or `GetMember(string)` methods that return property value for a given property name or index (e.g. in positional records). (Source Generators / Other)
README
# C# MemberAccessGenerator
see: [Source Generator](https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.md).
Original source (manually written):
```cs
using MemberAccess;
using System;[ByIndex]
partial record Point1(int X, int Y);[ByName]
partial record Point2(int X, int Y);namespace MemberAccessSample
{
[ByIndex, ByName]
partial record Point3(int X, int Y);partial class Program
{
static void Main()
{
var p1 = new Point1(1, 2);
Console.WriteLine((p1.GetMember(0), p1.GetMember(1)));var p2 = new Point2(1, 2);
Console.WriteLine((p2.GetMember("X"), p2.GetMember("Y")));var p3 = new Point3(1, 2);
Console.WriteLine((p3.GetMember(0), p3.GetMember(1)));
Console.WriteLine((p3.GetMember("X"), p3.GetMember("Y")));
}
}
}
```Generated source:
```cs
partial record Point1
{
public object GetMember(int index) => index switch
{
0 => X,
1 => Y,
_ => throw new System.Runtime.CompilerServices.SwitchExpressionException(),
};
}partial record Point2
{
public object GetMember(string name) => name switch
{
nameof(X) => X,
nameof(Y) => Y,
_ => throw new System.Runtime.CompilerServices.SwitchExpressionException(),
};
}namespace Sample {
partial record Point3
{
public object GetMember(int index) => index switch
{
0 => X,
1 => Y,
_ => throw new System.Runtime.CompilerServices.SwitchExpressionException(),
};public object GetMember(string name) => name switch
{
nameof(X) => X,
nameof(Y) => Y,
_ => throw new System.Runtime.CompilerServices.SwitchExpressionException(),
};
}
}
```