### Example of Generated C# XML Documentation Comment
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This C# code demonstrates the default XML documentation comment generated for a constructor, including summaries for parameters based on field summaries or parameter names.
```csharp
///
/// Initializes a new instance of the Test class.
///
/// Some field.
/// t2
```
--------------------------------
### Example of C# Code with Custom Constructor XML Documentation
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This C# code shows the result of applying a custom message to the constructor's XML documentation comment, as configured by `AutoConstructor_ConstructorDocumentationComment`.
```csharp
///
/// Some comment for the Test class.
///
/// Some field.
/// t2
```
--------------------------------
### APIDOC: AutoConstructorInitializerAttribute for Post-Construction Calls
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Documents the AutoConstructorInitializerAttribute, which allows specifying a parameterless, void-returning method to be called at the end of the generated constructor. This is useful for post-construction setup or initialization logic.
```APIDOC
AutoConstructorInitializerAttribute:
Purpose: Marks a parameterless, void-returning method to be called at the end of the generated constructor.
Usage: Apply to a method within the partial class marked with AutoConstructorAttribute.
Method Requirements:
- No parameters.
- Returns void.
```
--------------------------------
### Enable Parameterless Constructor Generation with AutoConstructor
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This C# example demonstrates how to use the `AutoConstructor` attribute with `addParameterless: true` to generate a parameterless constructor alongside the main constructor for a partial class.
```csharp
[AutoConstructor(addParameterless: true)]
public partial class Test
{
public int Id { get; set; }
public string Name { get; set; }
}
```
--------------------------------
### AutoConstructor Diagnostics Reference
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
A comprehensive reference for diagnostic codes (ACONSxx) issued by the AutoConstructor source generator. Each entry explains the condition that triggers the diagnostic and provides context or examples where applicable.
```APIDOC
ACONS01:
Description: The `AutoConstructor` attribute is used on a class that is not partial.
ACONS02:
Description: The `AutoConstructor` attribute is used on a class without fields to inject (without specifying `addParameterless` as true).
ACONS03:
Description: The `AutoConstructorIgnore` attribute is used on a field that won't already be processed.
ACONS04:
Description: The `AutoConstructorInject` attribute is used on a field that won't already be processed.
ACONS05:
Description: The `AutoConstructorIgnore` or `AutoConstructorInject` are used on a class without the `AutoConstructor` attribute.
ACONS06:
Description: A type specified in `AutoConstructorInject` attribute does not match the type of another parameter with the same name.
Example:
public partial class Test
{
[AutoConstructorInject("guid.ToString()", "guid", typeof(Guid))]
private readonly string _guid2;
private readonly string _guid;
}
ACONS07:
Description: The accessibility defined in the `AutoConstructor` attribute is not an allowed value.
ACONS08:
Description: `AutoConstructorInitializer` attribute used on multiple methods inside type.
ACONS09:
Description: `AutoConstructorInitializer` attribute used on a method not returning void.
ACONS10:
Description: `AutoConstructorInitializer` attribute used on a method with parameters.
ACONS11:
Description: `AutoConstructorDefaultBase` attribute used on multiple constructors inside type.
ACONS12:
Description: The `addParameterless` option is used on a type whose base type does not have a parameterless constructor.
ACONS99:
Description: `AutoConstructor_DisableNullChecking` is obsolete.
```
--------------------------------
### C# AutoConstructor: Configuring Default Base Constructor Call
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Demonstrates how to explicitly select a base constructor using the `[AutoConstructorDefaultBase]` attribute when a type has multiple base constructors. This attribute indicates which constructor should be chosen for the generated constructor's base call. The example shows the input classes and the resulting generated partial class.
```C#
internal class BaseClass
{
private readonly int _t;
[AutoConstructorDefaultBase]
public BaseClass(int t1, int t3)
{
this._t = t1 + t3;
}
public BaseClass(int t)
{
this._t = t;
}
public BaseClass()
{
}
}
[AutoConstructor]
internal partial class Test : BaseClass
{
private readonly int _t2;
}
```
```C#
partial class Test
{
public Test(int t2, int t1, int t3) : base(t1, t3)
{
this._t2 = t2;
}
}
```
--------------------------------
### Generated Parameterless Constructor Example
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This C# code shows the parameterless constructor generated by AutoConstructor when `addParameterless: true` is used. It includes an `[Obsolete]` attribute by default.
```csharp
partial class Test
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor.
[global::System.ObsoleteAttribute("Not intended for direct usage.", true)]
public Test()
{
}
}
```
--------------------------------
### C# Class Definition with AutoConstructor Attributes
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Defines a C# partial class `Test` decorated with `AutoConstructor` and various `AutoConstructorInject` and `AutoConstructorIgnore` attributes. This example demonstrates how different property configurations (get-only, static, initialized, explicitly injected, ignored, and custom-initialized) influence constructor parameter generation.
```csharp
[AutoConstructor]
public partial class Test
{
[field: AutoConstructorInject]
public int Injected { get; }
public int AlsoInjectedEvenWhenMissingAttribute { get; }
///
/// Some property.
///
[field: AutoConstructorInject]
public int InjectedWithDocumentation { get; }
[field: AutoConstructorInject]
public int InjectedBecauseExplicitInjection { get; set; }
[field: AutoConstructorInject]
public static int NotInjectedBecauseStatic { get; }
[field: AutoConstructorInject]
public int NotInjectedBecauseInitialized { get; } = 2;
[field: AutoConstructorIgnore]
public int NotInjectedBecauseHasIgnoreAttribute { get; }
[field: AutoConstructorInject(initializer: "injected.ToString()", injectedType: typeof(int), parameterName: "injected")]
public string InjectedWithoutCreatingAParam { get; }
}
```
--------------------------------
### Generated Constructor from Field Injection Example
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This C# code shows the constructor generated by AutoConstructor based on the `Test` class definition. It demonstrates how readonly fields, custom initializers, and parameter reuse are handled during constructor generation.
```csharp
//------------------------------------------------------------------------------
//
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
partial class Test
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoConstructor", "5.0.0.0")]
public Test(string name, global::System.DateTime? date, global::System.Collections.Generic.List items, global::System.Guid guid)
{
this._name = name ?? throw new global::System.ArgumentNullException(nameof(name));
this._date = date;
this._items = items ?? throw new global::System.ArgumentNullException(nameof(items));
this._guidString = guid.ToString() ?? throw new global::System.ArgumentNullException(nameof(guid));
this._guidLength = guid.ToString().Length;
this._nameShared = name.ToUpper() ?? throw new global::System.ArgumentNullException(nameof(name));
}
}
```
--------------------------------
### Example Class Definition for AutoConstructor Field Injection
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This C# class demonstrates various field types and attributes (`AutoConstructorIgnore`, `AutoConstructorInject`) that influence how AutoConstructor injects fields into the generated constructor. It shows support for readonly fields, nullables, generics, and custom initializers.
```csharp
[AutoConstructor]
partial class Test
{
private readonly string _name;
// Won't be injected
private readonly Uri _uri = new Uri("/non-modified", UriKind.Relative);
// Won't be injected
[AutoConstructorIgnore]
private readonly DateTime _dateNotTaken;
// Won't be injected because not readonly. Attribute would be taken into account if this were a property, not a field.
[AutoConstructorInject]
private int _stuff;
// Won't be injected
private int? _toto;
// Support for nullables
private readonly DateTime? _date;
// Support for generics
private readonly List _items;
// Inject with custom initializer
[AutoConstructorInject("guid.ToString()", "guid", typeof(Guid))]
private readonly string _guidString;
// Use existing parameter defined with AutoConstructorInject
[AutoConstructorInject("guid.ToString().Length", "guid", typeof(Guid))]
private readonly int _guidLength;
// Use existing parameter from a basic injection
[AutoConstructorInject("name.ToUpper()", "name", typeof(string))]
private readonly string _nameShared;
}
```
--------------------------------
### C# AutoConstructor: Customizing Property Injection
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Provides an example of how to use the `[field: AutoConstructorInject]` attribute with parameters like `initializer`, `injectedType`, and `parameterName` to customize the injection of auto-implemented properties. This allows for fine-grained control over how properties are initialized by the generated constructor.
```C#
[field: AutoConstructorInject(initializer: "injected.ToString()", injectedType: typeof(int), parameterName: "injected")]
public int Property { get; }
```
--------------------------------
### AutoConstructor Analyzer Rules for Release 5.0.0
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/src/AutoConstructor.Generator/AnalyzerReleases.Shipped.md
Details new rules introduced in AutoConstructor analyzer version 5.0.0, including ACONS07, ACONS08, ACONS09, and ACONS10. These rules primarily focus on constructor accessibility and initializer methods.
```APIDOC
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ACONS07 | Usage | Warning | TypeWithWrongConstructorAccessibilityAnalyzer
ACONS08 | Usage | Warning | InitializerMethodAnalyzer
ACONS09 | Usage | Error | InitializerMethodAnalyzer
ACONS10 | Usage | Error | InitializerMethodAnalyzer
```
--------------------------------
### Configure XML Documentation Generation for Constructors
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This XML snippet shows how to enable the generation of XML documentation comments for constructors by setting `AutoConstructor_GenerateConstructorDocumentation` to `true` in the project file.
```xml
true
```
--------------------------------
### C# AutoConstructor: Input Class Definition
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Demonstrates the basic usage of the AutoConstructor source generator by defining a partial class with readonly fields and applying the AutoConstructor attribute. It also shows how to use AutoConstructorInject for custom parameter injection, allowing different parameter names or types than the field itself.
```csharp
[AutoConstructor]
public partial class MyClass
{
private readonly MyDbContext _context;
private readonly IHttpClientFactory _clientFactory;
private readonly IService _service;
[AutoConstructorInject("options?.Value", "options", typeof(IOptions))]
private readonly ApplicationOptions _options;
}
```
--------------------------------
### XML Configuration: Enabling ArgumentNullException Checks
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Shows how to configure the project file to enable the generation of `ArgumentNullException` checks for constructor parameters. By setting `AutoConstructor_GenerateArgumentNullExceptionChecks` to `true`, the library will emit null checks where appropriate. An older configuration option is also provided for reference.
```XML
true
```
```XML
false
```
--------------------------------
### AutoConstructor Analyzer Rules for Release 1.0.0
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/src/AutoConstructor.Generator/AnalyzerReleases.Shipped.md
Details the initial set of rules (ACONS01 through ACONS05) introduced in the first release of the AutoConstructor analyzer. These rules cover various usage patterns related to partial types, field injection, and attribute usage.
```APIDOC
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ACONS01 | Usage | Warning | TypeWithoutPartialAnalyzer
ACONS02 | Usage | Warning | TypeWithoutFieldsToInjectAnalyzer
ACONS03 | Usage | Info | IgnoreAttributeOnNonProcessedFieldAnalyzer
ACONS04 | Usage | Warning | InjectAttributeOnIgnoredFieldAnalyzer
ACONS05 | Usage | Warning | IgnoreOrInjectAttributeOnTypeWithoutAttributeAnalyzer
```
--------------------------------
### C# AutoConstructor: Base Parameter Matching by Name and Type
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Illustrates the default behavior of AutoConstructor where parameters with the same name and type in both the parent and child classes are automatically matched and passed to the base constructor. This simplifies constructor generation by reusing existing parameters.
```C#
// This (same name and type)
public class ParentClass
{
private readonly int value;
public ParentClass(int value)
{
this.value = value;
}
}
[AutoConstructor]
public partial class Test : ParentClass
{
private readonly int value;
}
```
```C#
partial class Test
{
public Test(int service) : base(service)
{
this.service = service;
}
}
```
--------------------------------
### Generated C# Constructor by AutoConstructor
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Illustrates the C# constructor automatically generated by the `AutoConstructor` source generator based on the provided `Test` class definition. It shows how properties marked for injection are transformed into constructor parameters, including those with custom initializers.
```csharp
//------------------------------------------------------------------------------
//
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
partial class Test
{
///
/// Initializes a new instance of the Test class.
///
/// injected
/// alsoInjectedEvenWhenMissingAttribute
/// Some property.
/// injectedBecauseExplicitInjection
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoConstructor", "5.0.0.0")]
public Test(int injected, int alsoInjectedEvenWhenMissingAttribute, int injectedWithDocumentation, int injectedBecauseExplicitInjection)
{
this.Injected = injected;
this.AlsoInjectedEvenWhenMissingAttribute = alsoInjectedEvenWhenMissingAttribute;
this.InjectedWithDocumentation = injectedWithDocumentation;
this.InjectedBecauseExplicitInjection = injectedBecauseExplicitInjection;
this.InjectedWithoutCreatingAParam = injected.ToString() ?? throw new global::System.ArgumentNullException(nameof(injected));
}
}
```
--------------------------------
### C# AutoConstructor: Input Class with Initializer Method
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Illustrates how to define a partial class with a method marked by AutoConstructorInitializerAttribute. This method will be automatically called after the constructor's field assignments, enabling custom initialization logic post-construction.
```csharp
[AutoConstructor]
internal partial class Test
{
private readonly int _t;
[AutoConstructorInitializer]
public void Initializer()
{
}
}
```
--------------------------------
### C# AutoConstructor: Generated Constructor Output
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Shows the C# code generated by the AutoConstructor source generator based on the input class definition. This includes the automatically generated constructor with injected dependencies for readonly fields and custom parameter handling as specified by AutoConstructorInject.
```csharp
//------------------------------------------------------------------------------
//
// This code was generated by the AutoConstructor source generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
partial class MyClass
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("AutoConstructor", "5.0.0.0")]
public MyClass(global::MyDbContext context, global::System.Net.Http.IHttpClientFactory clientFactory, global::IService service, global::Microsoft.Extensions.Options.IOptions options)
{
this._context = context;
this._clientFactory = clientFactory;
this._service = service;
this._options = options?.Value;
}
}
```
--------------------------------
### AutoConstructor Analyzer Rules for Release 5.3.0
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/src/AutoConstructor.Generator/AnalyzerReleases.Shipped.md
Details new rules introduced in AutoConstructor analyzer version 5.3.0, including ACONS11 and ACONS99. These rules cover usage patterns and are associated with DefaultBaseAttributeAnalyzer and AutoConstructorGenerator.
```APIDOC
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ACONS11 | Usage | Error | DefaultBaseAttributeAnalyzer
ACONS99 | Usage | Warning | AutoConstructorGenerator
```
--------------------------------
### C# AutoConstructor: Base Parameter Matching with Type Mismatch
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Shows how AutoConstructor handles base constructor parameter matching when parameters have the same name but different types. In such cases, the parameters are not matched, and distinct parameters are generated for both the child and base constructors.
```C#
// This (same name but not same type)
public class ParentClass
{
private readonly long value;
public ParentClass(long value)
{
this.value = value;
}
}
[AutoConstructor]
public partial class Test : ParentClass
{
private readonly int value;
}
```
```C#
partial class Test
{
public Test(int service, long b0__service) : base(b0__service)
{
this.service = service;
}
}
```
--------------------------------
### AutoConstructor Analyzer Rules for Release 1.2.0
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/src/AutoConstructor.Generator/AnalyzerReleases.Shipped.md
Details the new rule ACONS06 introduced in AutoConstructor analyzer version 1.2.0, related to the AutoConstructorGenerator.
```APIDOC
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
ACONS06 | Usage | Error | AutoConstructorGenerator
```
--------------------------------
### Customize Constructor XML Documentation Comment Message
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
This XML configuration allows customizing the message used in the constructor's XML documentation comment by setting the `AutoConstructor_ConstructorDocumentationComment` property.
```xml
Some comment for the {0} class.
```
--------------------------------
### C# AutoConstructor: Generated Constructor with Initializer Call
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Shows the C# code generated by AutoConstructor when an initializer method is specified using AutoConstructorInitializerAttribute. The generated constructor includes a call to the designated initializer method after all field assignments, demonstrating the execution flow.
```csharp
public Test(int t)
{
this._t = t;
this.Initializer();
}
```
--------------------------------
### C# AutoConstructor: Forcing Base Parameter Matching by Name Only
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Demonstrates how to override the default type-matching behavior by setting the `matchBaseParameterOnName` parameter on `AutoConstructorAttribute` to `true`. This allows parameters to be matched solely by name, even if their types differ, though it carries a risk of generating invalid code.
```C#
// This (same name but not same type with matchBaseParameterOnName true)
public class ParentClass
{
private readonly long value;
public ParentClass(long value)
{
this.value = value;
}
}
[AutoConstructor(matchBaseParameterOnName: true)]
public partial class Test : ParentClass
{
private readonly int value;
}
```
```C#
partial class Test
{
public Test(int service) : base(service)
{
this.service = service;
}
}
```
--------------------------------
### APIDOC: AutoConstructorInjectAttribute Customization
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Documents the AutoConstructorInjectAttribute, used to customize the injection behavior for specific readonly fields. It explains the 'initializer', 'parameterName', and 'injectedType' parameters for fine-grained control over constructor parameter and field initialization.
```APIDOC
AutoConstructorInjectAttribute:
Purpose: Customizes the injection behavior for a specific readonly field.
Usage: Apply to a readonly field within a class marked with AutoConstructorAttribute.
Parameters:
initializer (string, optional):
Description: A string expression used to initialize the field.
Default: parameterName (if null or empty)
Example: "myService.GetData()"
parameterName (string, optional):
Description: The name of the parameter to be used in the constructor.
Default: Field name (trimmed, if null or empty)
Example: "myService"
injectedType (Type, optional):
Description: The type of the parameter to be used in the constructor.
Default: Field type (if null)
Example: "IMyService"
Notes:
- If no parameters are provided, behavior is same as without the attribute.
- Applying to a non-injectable field won't make it injectable.
- Parameter names can be shared across fields if types match.
```
--------------------------------
### APIDOC: AutoConstructorAttribute Configuration
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Documents the AutoConstructorAttribute, which is used to mark classes or structs for automatic constructor generation. It details the optional 'accessibility' parameter for controlling the generated constructor's visibility.
```APIDOC
AutoConstructorAttribute:
Purpose: Marks a class or struct for automatic constructor generation.
Usage: Apply to a partial class or struct.
Parameters:
accessibility (string, optional):
Description: Controls the accessibility of the generated constructor.
Default: "public"
Allowed Values: "public", "private", "protected", "internal", "protected internal", "private protected"
```
--------------------------------
### Configure Obsolete Attribute for Parameterless Constructor
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
These XML properties allow customizing the `[Obsolete]` attribute behavior for the generated parameterless constructor, including disabling it or setting a custom message.
```xml
false
Custom obsolete message
```
--------------------------------
### XML Configuration: Disabling Generated `this()` Calls
Source: https://github.com/k94ll13nn3/autoconstructor/blob/main/README.md
Illustrates how to prevent AutoConstructor from generating `this()` calls to existing parameterless constructors. By setting `AutoConstructor_GenerateThisCalls` to `false` in the project file, this behavior can be globally disabled. This can also be configured at the attribute level.
```XML
false
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.