SkillAgentSearch skills...

AutoCtor

A Roslyn source generator for creating constructors.

Install / Use

/learn @distantcam/AutoCtor
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<!-- GENERATED FILE - DO NOT EDIT This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets). Source File: /readme.source.md To change this file edit the source file and then run MarkdownSnippets. -->

AutoCtor

Build Status NuGet Status Nuget Downloads

AutoCtor is a Roslyn Source Generator that will automatically create a constructor for your class for use with constructor Dependency Injection.

+[AutoConstruct]
public partial class AService
{
    private readonly IDataContext _dataContext;
    private readonly IDataService _dataService;
    private readonly IExternalService _externalService;
    private readonly ICacheService _cacheService;
    private readonly ICacheProvider _cacheProvider;
    private readonly IUserService _userService;

-    public AService(
-        IDataContext dataContext,
-        IDataService dataService,
-        IExternalService externalService,
-        ICacheService cacheService,
-        ICacheProvider cacheProvider,
-        IUserService userService
-    )
-    {
-        _dataContext = dataContext;
-        _dataService = dataService;
-        _externalService = externalService;
-        _cacheService = cacheService;
-        _cacheProvider = cacheProvider;
-        _userService = userService;
-    }
}

Star History

Star History Chart

<a id='toc'></a>

<!-- toc -->

Contents

NuGet packages

https://nuget.org/packages/AutoCtor/

Examples

Basic

<!-- snippet: Basic -->

<a id='snippet-Basic'></a>

[AutoConstruct]
public partial class Basic
{
    private readonly IService _service;
    private readonly IList<string> _list = new List<string>();
}

<sup><a href='/src/Tests/ReadmeExamples/Basic.cs#L6-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-Basic' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: Basic.cs#Basic.g.verified.cs -->

<a id='snippet-Basic.cs#Basic.g.verified.cs'></a>

//HintName: Basic.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------

partial class Basic
{
	[global::System.Runtime.CompilerServices.CompilerGenerated]
	[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
	[global::System.Diagnostics.DebuggerNonUserCode]
	[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
	public Basic(global::IService service)
	{
		this._service = service;
	}
}

<sup><a href='/src/Tests/ReadmeExamples/Basic.cs%23Basic.g.verified.cs#L1-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-Basic.cs#Basic.g.verified.cs' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> </details>

Inherited

<!-- snippet: Inherited -->

<a id='snippet-Inherited'></a>

public abstract class BaseClass
{
    protected IAnotherService _anotherService;

    public BaseClass(IAnotherService anotherService)
    {
        _anotherService = anotherService;
    }
}

[AutoConstruct]
public partial class Inherited : BaseClass
{
    private readonly IService _service;
}

<sup><a href='/src/Tests/ReadmeExamples/Inherited.cs#L6-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-Inherited' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: Inherited.cs#Inherited.g.verified.cs -->

<a id='snippet-Inherited.cs#Inherited.g.verified.cs'></a>

//HintName: Inherited.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------

partial class Inherited
{
	[global::System.Runtime.CompilerServices.CompilerGenerated]
	[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
	[global::System.Diagnostics.DebuggerNonUserCode]
	[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
	public Inherited(
		global::IAnotherService anotherService,
		global::IService service
	) : base(anotherService)
	{
		this._service = service;
	}
}

<sup><a href='/src/Tests/ReadmeExamples/Inherited.cs%23Inherited.g.verified.cs#L1-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-Inherited.cs#Inherited.g.verified.cs' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> </details>

Properties

<!-- snippet: Properties -->

<a id='snippet-Properties'></a>

// AutoCtor will initialize these
public string GetProperty { get; }
protected string ProtectedProperty { get; }
public string InitProperty { get; init; }
public required string RequiredProperty { get; set; }

// AutoCtor will ignore these
public string InitializerProperty { get; } = "Constant";
public string GetSetProperty { get; set; }
public string FixedProperty => "Constant";
public string RedirectedProperty => InitializerProperty;

<sup><a href='/src/Tests/ReadmeExamples/Properties.cs#L4-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-Properties' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: Properties.cs#Properties.g.verified.cs -->

<a id='snippet-Properties.cs#Properties.g.verified.cs'></a>

//HintName: Properties.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------

partial class Properties
{
	[global::System.Runtime.CompilerServices.CompilerGenerated]
	[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
	[global::System.Diagnostics.DebuggerNonUserCode]
	[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
	public Properties(
		string getProperty,
		string protectedProperty,
		string initProperty,
		string requiredProperty
	)
	{
		this.GetProperty = getProperty;
		this.ProtectedProperty = protectedProperty;
		this.InitProperty = initProperty;
		this.RequiredProperty = requiredProperty;
	}
}

<sup><a href='/src/Tests/ReadmeExamples/Properties.cs%23Properties.g.verified.cs#L1-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-Properties.cs#Properties.g.verified.cs' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> </details>

<a href='#toc' title='Back to Contents'>Back to Contents</a>

Post Constructor Initialization

You can mark a method to be called at the end of the constructor with the attribute [AutoPostConstruct]. This method must return void.

<!-- snippet: PostConstruct -->

<a id='snippet-PostConstruct'></a>

[AutoConstruct]
public partial class PostConstruct
{
    private readonly IService _service;

    [AutoPostConstruct]
    private void Initialize()
    {
    }
}

<sup><a href='/src/Tests/ReadmeExamples/PostConstruct.cs#L5-L18' title='Snippet source file'>snippet source</a> | <a href='#snippet-PostConstruct' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> <details><summary>What gets generated</summary> <!-- snippet: PostConstruct.cs#PostConstruct.g.verified.cs -->

<a id='snippet-PostConstruct.cs#PostConstruct.g.verified.cs'></a>

//HintName: PostConstruct.g.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by https://github.com/distantcam/AutoCtor
// </auto-generated>
//------------------------------------------------------------------------------

partial class PostConstruct
{
	[global::System.Runtime.CompilerServices.CompilerGenerated]
	[global::System.CodeDom.Compiler.GeneratedCode("AutoCtor", "0.0.0.0")]
	[global::System.Diagnostics.DebuggerNonUserCode]
	[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
	public PostConstruct(global::IService service)
	{
		this._service = service;
		Initialize();
	}
}

<sup><a href='/src/Tests/ReadmeExamples/PostConstruct.cs%23PostConstruct.g.verified.cs#L1-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-PostConstruct.cs#PostConstruct.g.verified.cs' title='Start of snippet'>anchor</a></sup>

<!-- endSnippet --> </details>

Extra Parameters

Post construct methods can also take parameters. The generated constructor will include these parameters.

<!-- snippet: PostConstructWithParameter -->

<a id='snippet-PostConstructWithParameter'></a>

[AutoConstruct]
public partial class PostConstructWithParameter
{
   

Related Skills

View on GitHub
GitHub Stars111
CategoryDevelopment
Updated23h ago
Forks4

Languages

C#

Security Score

100/100

Audited on Mar 28, 2026

No findings