Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chaowlert/PrimaryConstructor
Generate primary constructor from readonly fields
https://github.com/chaowlert/PrimaryConstructor
csharp-sourcegenerator primary-constructor
Last synced: about 1 month ago
JSON representation
Generate primary constructor from readonly fields
- Host: GitHub
- URL: https://github.com/chaowlert/PrimaryConstructor
- Owner: chaowlert
- License: mit
- Created: 2020-10-26T14:54:23.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-26T10:17:57.000Z (11 months ago)
- Last Synced: 2024-11-06T22:53:38.987Z (about 1 month ago)
- Topics: csharp-sourcegenerator, primary-constructor
- Language: C#
- Homepage:
- Size: 37.1 KB
- Stars: 105
- Watchers: 4
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - PrimaryConstructor
- csharp-source-generators - PrimaryConstructor - ![stars](https://img.shields.io/github/stars/chaowlert/PrimaryConstructor?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/chaowlert/PrimaryConstructor?style=flat-square&cacheSeconds=86400) Generate primary constructor from readonly fields. (Source Generators / Other)
README
# PrimaryConstructor
Generate primary constructor from readonly fields & properties[![NuGet](https://img.shields.io/nuget/v/PrimaryConstructor.svg)](https://www.nuget.org/packages/PrimaryConstructor)
![image](https://user-images.githubusercontent.com/5763993/97197488-4b65ad80-17e0-11eb-9eef-305ce284eb78.png)
### Get it
```
PM> Install-Package PrimaryConstructor
```### Prerequisites
Visual Studio version 16.8 and above is required as its first version to support source generators.
### Usage
Declare class with `partial`, and annotate with `[PrimaryConstructor]`.
And then you can declare your dependencies with readonly fields.```csharp
[PrimaryConstructor]
public partial class MyService
{
private readonly MyDependency _myDependency;...
}
```When compile, following source will be injected.
```csharp
partial class MyService
{
public MyService(MyDependency myDependency)
{
this._myDependency = myDependency;
}
}
```### Rules
1. **readonly** fields & properties **without initializer** will be automatically injected.
2. if the service has based type, based type must also annotated with `PrimaryConstructor` in order to inject members from based type.
3. you can exclude members from injection by annotated with `IgnorePrimaryConstructor`.
4. you can include members to injection by annotated with `IncludePrimaryConstructor`.### Emit generated files
Visual Studio still not fully support source generator, it sometimes shows error marker on symbols referred to the generated code.
Emitting the generated files will allow you to see the code, and also solve Visual Studio error marker problem.To emit generated files, add following code to your `csproj` file.
```xml
$(MSBuildProjectDirectory)/generated
true
```
Please check [PrimaryConstructor.Sample.csproj](https://github.com/chaowlert/PrimaryConstructor/blob/main/PrimaryConstructor.Sample/PrimaryConstructor.Sample.csproj) for sample.