MFToolkit.Abstractions 1.0.20

dotnet add package MFToolkit.Abstractions --version 1.0.20
                    
NuGet\Install-Package MFToolkit.Abstractions -Version 1.0.20
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MFToolkit.Abstractions" Version="1.0.20" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MFToolkit.Abstractions" Version="1.0.20" />
                    
Directory.Packages.props
<PackageReference Include="MFToolkit.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MFToolkit.Abstractions --version 1.0.20
                    
#r "nuget: MFToolkit.Abstractions, 1.0.20"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MFToolkit.Abstractions@1.0.20
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MFToolkit.Abstractions&version=1.0.20
                    
Install as a Cake Addin
#tool nuget:?package=MFToolkit.Abstractions&version=1.0.20
                    
Install as a Cake Tool

MFToolkit.Abstractions - 依赖注入特性定义库

概述

本库提供了一套完整的依赖注入特性定义,用于标记需要自动注入的服务类。仅支持 .NET 9.0 及以上版本。

安装要求


<PackageReference Include="MFToolkit.Abstractions" Version="1.0.20" />
<PackageReference Include="MFToolkit.AutoGenerator" Version="1.0.20" />

核心特性

AutoInjectAttribute

// 不推荐使用,建议使用具体的生命周期特性

具体生命周期特性

  • SingletonAttribute - 单例服务
  • ScopedAttribute - 作用域服务
  • TransientAttribute - 瞬态服务
  • TrySingletonAttribute - TryAdd 单例服务
  • TryScopedAttribute - TryAdd 作用域服务
  • TryTransientAttribute - TryAdd 瞬态服务

使用示例

基础用法

// 泛型形式 - 推荐
[Singleton<IMyService>]
public class MyService : IMyService { }

[Scoped<IRepository>]
public class Repository : IRepository { }

// 非泛型形式
[Singleton(typeof(IMyService))]
public class MyService : IMyService { }

带 Key 的注入

// 字符串 Key
[Singleton<IMyService>("service1")]
public class MyService1 : IMyService { }

[Singleton<IMyService>("service2")]
public class MyService2 : IMyService { }

// Type 作为 Key(特殊规则)
[Singleton(typeof(string))] // 如果类不继承 string,typeof(string) 会被当作 Key
public class StringKeyService { }

服务名称自定义

[AutoInjectServiceName("AddMyCustomServices")]
[Singleton<IMyService>]
public class MyService : IMyService { }

重要规则说明

Type 参数的特殊处理规则:

  • 如果指定的 Type 是当前类的基类或实现的接口 → 作为服务类型
  • 如果指定的 Type 不是有效的服务类型 → 作为服务 Key

示例说明:

// ✅ 正确:IMyService 是当前类实现的接口 → 作为服务类型
[Singleton(typeof(IMyService))]
public class MyService : IMyService { }

// 🔑 特殊:string 不是当前类的基类 → typeof(string) 作为 Key
[Singleton(typeof(string))]
public class MyService { }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.20 308 11/12/2025
1.0.19 192 10/15/2025
1.0.18 188 10/10/2025
1.0.17 194 10/10/2025