Fluentish.InjectableStatic.Generator
0.2.1
dotnet add package Fluentish.InjectableStatic.Generator --version 0.2.1
NuGet\Install-Package Fluentish.InjectableStatic.Generator -Version 0.2.1
<PackageReference Include="Fluentish.InjectableStatic.Generator" Version="0.2.1" />
<PackageVersion Include="Fluentish.InjectableStatic.Generator" Version="0.2.1" />
<PackageReference Include="Fluentish.InjectableStatic.Generator" />
paket add Fluentish.InjectableStatic.Generator --version 0.2.1
#r "nuget: Fluentish.InjectableStatic.Generator, 0.2.1"
#addin nuget:?package=Fluentish.InjectableStatic.Generator&version=0.2.1
#tool nuget:?package=Fluentish.InjectableStatic.Generator&version=0.2.1
Fluentish.InjectableStatic.Generator
When upstream doesn't like inversion of control
Fluentish.InjectableStatic.Generator
is a source generator for C#. It allows you to generate interface and service classes that wrap static members of your target classes. By applying attributes, you can control which static classes and members become “injectable,” making it easier to work with otherwise rigid static code through a dependency injection container.
Features
- Automatically generate wrapper classes and corresponding interfaces
- open generics support
- Supported member types:
- Constants
- static Properties
- static Events
- static Fields
- static Methods
- Filter which members are included in generated code
- Customize namespace prefix
Usage
Basic usage:
[assembly: Fluentish.InjectableStatic.Injectable(typeof(System.Console))]
Fluentish.Injectable.System.IConsole console = new Fluentish.Injectable.System.ConsoleService();
console.WriteLine("Hello, World!");
Will generate Fluentish.Injectable.System.IConsole
and Fluentish.Injectable.System.ConsoleService
, which contain all the static members.
Filtering members:
- Exclude
Will generate[assembly: Fluentish.InjectableStatic.Injectable( typeof(System.Diagnostics.Debug), Fluentish.InjectableStatic.FilterType.Exclude, nameof(System.Diagnostics.Debug.Print) )]
Fluentish.Injectable.System.Diagnostics.IDebug
andFluentish.Injectable.System.Diagnostics.DebugService
without thePrint
method. - Include
Will generate[assembly: Fluentish.InjectableStatic.Injectable( typeof(System.Diagnostics.Debug), Fluentish.InjectableStatic.FilterType.Include, nameof(System.Diagnostics.Debug.Write), nameof(System.Diagnostics.Debug.WriteIf), nameof(System.Diagnostics.Debug.WriteLine), nameof(System.Diagnostics.Debug.WriteLineIf) )]
Fluentish.Injectable.System.Diagnostics.IDebug
andFluentish.Injectable.System.Diagnostics.DebugService
containing only the listed members.
Configuration
By default, generated classes are prefixed with Fluentish.Injectable.
to avoid name collisions. You can customize this behavior using the InjectableStaticConfigurationAttribute
at the assembly level.
Available Options
[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(
Namespace = "Your.Custom.Namespace",
NamespaceMode = Fluentish.InjectableStatic.NamespaceMode.Prefix // or Fluentish.InjectableStatic.NamespaceMode.Const
)]
Property | Description |
---|---|
Namespace |
The base namespace for generated wrappers. Set to an empty string ("" ) to disable namespace prefixing entirely. |
NamespaceMode |
Determines how the namespace is applied. See below for details. |
Namespace Modes
NamespaceMode.Prefix
(default):
The value ofNamespace
is used as a prefix. For example, if set to"My.Custom"
, generated classes will be in"My.Custom.Original.Namespace"
.NamespaceMode.Const
:
The value ofNamespace
replaces the entire namespace. All generated classes will reside in exactly the specified namespace.
Examples
Remove the default prefix:
[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(Namespace = "")]
Use a custom namespace as a prefix:
[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(
Namespace = "My.Namespace",
NamespaceMode = NamespaceMode.Prefix
)]
Force all wrappers into a constant namespace:
[assembly: Fluentish.InjectableStatic.InjectableStaticConfigurationAttribute(
Namespace = "My.Constant.Namespace",
NamespaceMode = NamespaceMode.Const
)]
Remarks
Please note that this project is in its early stages of development and may require significant improvements. While it is functional, it may not be suitable for production use in its current state. Any feedback, suggestions, or contributions to improve the source generator are highly appreciated.
Contributing
All contributions are appreciated, but please discuss any ideas or planned changes with me through a GitHub issue beforehand to avoid unnecessary or conflicting work.
Support
If you have any questions, issues, or suggestions, please create a new issue on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for more information.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Fluentish.InjectableStatic.Generator:
Package | Downloads |
---|---|
Godot.DependencyInjection.Services
part of custom dependency injection for Godot game engine |
GitHub repositories
This package is not used by any popular GitHub repositories.