You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using `Microsoft.Extensions.DependencyInjection` you can mark fields and properties with `[AutoKeyedService]` and it will be included in the constructor.
90
+
</details>
91
+
92
+
### Extra Parameters
71
93
72
-
snippet: KeyedService
94
+
Post construct methods can also take parameters. The generated constructor will include these parameters.
73
95
74
-
snippet: KeyedServiceGeneratedCode
96
+
snippet: PostConstructWithParameter
75
97
76
-
### Initialize with parameters
98
+
<details><summary>What gets generated</summary>
77
99
78
-
Post constructor methods can also take parameters. These parameters will be passed in from the constructor.
Parameters marked with `out` or `ref` are set back to any fields that match the same type. This can be used to set readonly fields with more complex logic.
85
107
86
-
If a parameter is marked `ref` or `out` and matches the type of a readonly field, it can set that field during construction.
<ahref='#toc'title='Back to Contents'>Back to Contents</a>
121
-
## More examples
146
+
## Keyed Services
122
147
123
-
You can also initialise readonly fields, and AutoCtor will not include them in the constructor.
124
-
125
-
snippet: PresetField
148
+
When using `Microsoft.Extensions.DependencyInjection` you can mark fields and properties with `[AutoKeyedService]` and it will be included in the constructor.
126
149
127
-
snippet: PresetFieldGeneratedCode
150
+
snippet: Keyed
128
151
129
-
If there is a single base constructor with parameters, AutoCtor will include that base constructor in the constructor it creates.
<ahref='#toc'title='Back to Contents'>Back to Contents</a>
136
-
## Embedding the attributes in your project
159
+
## Other
160
+
161
+
### Embedding The Attributes
137
162
138
163
By default, the `[AutoConstruct]` attributes referenced in your project are contained in an external dll. It is also possible to embed the attributes directly in your project. To do this, you must do two things:
139
164
140
-
1. Define the MSBuild constant `AUTOCTOR_EMBED_ATTRIBUTES`. This ensures the attributes are embedded in your project.
165
+
1. Define the constant `AUTOCTOR_EMBED_ATTRIBUTES`. This ensures the attributes are embedded in your project.
141
166
2. Add `compile` to the list of excluded assets in your `<PackageReference>` element. This ensures the attributes in your project are referenced, instead of the _AutoCtor.Attributes.dll_ library.
142
167
143
168
Your project file should look like this:
@@ -147,7 +172,7 @@ Your project file should look like this:
The `[AutoConstruct]` attributes are decorated with the `[Conditional]` attribute, so their usage will not appear in the build output of your project. If you use reflection at runtime you will not find the `[AutoConstruct]` attributes.
166
196
167
-
If you wish to preserve these attributes in the build output, you can define the`AUTOCTOR_USAGES` MSBuild variable.
197
+
If you wish to preserve these attributes in the build output, add the define constant`AUTOCTOR_USAGES`.
0 commit comments