JCoder.Ioc4Net 3.2604.1

dotnet add package JCoder.Ioc4Net --version 3.2604.1
                    
NuGet\Install-Package JCoder.Ioc4Net -Version 3.2604.1
                    
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="JCoder.Ioc4Net" Version="3.2604.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JCoder.Ioc4Net" Version="3.2604.1" />
                    
Directory.Packages.props
<PackageReference Include="JCoder.Ioc4Net" />
                    
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 JCoder.Ioc4Net --version 3.2604.1
                    
#r "nuget: JCoder.Ioc4Net, 3.2604.1"
                    
#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 JCoder.Ioc4Net@3.2604.1
                    
#: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=JCoder.Ioc4Net&version=3.2604.1
                    
Install as a Cake Addin
#tool nuget:?package=JCoder.Ioc4Net&version=3.2604.1
                    
Install as a Cake Tool

JCoder.Ioc4Net

目录

[TOC]

一.架构说明

JCoder.Ioc4Net
语言 C#
协议 Apache-2.0
.Net框架 .Net Standard2.1; .Net Framework 4.7.2; .Net 6; .Net 8; .Net 10;
简述 A simple IOC library.(一款简易的IOC操作类库。)

1.1 引用类库

引用类库 版本
- -

1.2 相关类库

| 相关类库 | 说明 | | ---------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | | JCoder | The base library of JCoder Serial.(JCoder库系列的基础库。) | | JCoder.Aop4Net | AOP框架,用于实现AOP功能。 | | JCoder.CoreKits | A simple library for C# extension.(一款简易的用于C#的扩展库。) | | JCoder.Data | A library of generic data objects.(一款通用的数据对象库。) | | JCoder.Mock4Net | A simple library for data mock.(一款简易版的用于模拟数据的功能库。) |

获取方式

  1. 版本要求:>=3.2603.1
  2. 以版本3.2603.1为例
获取方式 描述
Package Manager Install-Package JCoder.Ioc4Net -Version 3.2603.1
.Net CLI dotnet add package JCoder.Ioc4Net --version 3.2603.1
PackageReference <PackageReference Include="JCoder.Ioc4Net" Version="3.2603.1" />
Paket CLI paket add JCoder.Ioc4Net --version 3.2603.1
Script&Interactive #r "nuget: JCoder.Ioc4Net, 3.2603.1"
Cake // Install JCoder.Ioc4Net as a Cake Addin<br/>#addin nuget:?package=JCoder.Ioc4Net&version=3.2603.1<br/><br/>// Install JCoder.Ioc4Net as a Cake Tool<br/>#tool nuget:?package=JCoder.Ioc4Net&version=3.2603.1

使用说明

一、创建容器(IocCastle)

using JCoder.Ioc4Net;

IIocCastle _iocCastle = new IocCastle();

容器默认:

  • 注册模式:IocRegisterMode.Override(覆盖注册,后注册者生效);
  • 注册目标:IocRTarget.Interface | IocRTarget.Class(同时注册接口与类继承链)。

可通过 SetMode(...) / SetTypeTargets(...) 修改,或通过 GetConfig() 查看当前配置。

二、注册程序集

  1. 直接注册 Assembly
using JCoder.Ioc4Net;

IIocCastle _iocCastle = new IocCastle();
var _assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var a in _assemblies)
{
    _iocCastle.RegisterAssembly(a);
}
  1. 从文件夹注册程序集(可配合文件过滤器)
IIocCastle _iocCastle = new IocCastle();

// 只注册 *.dll 文件
_iocCastle.RegisterFromFolder(@"D:\MyPlugins", "*.dll", true);
  1. 添加过滤(加载Assembly / 类型)
IIocCastle _iocCastle = new IocCastle();

//添加文件过滤(根据文件名称)
_iocCastle.AddFilter(new IocNameFileFilter
{
    Prefix = "JCoder.Ioc4Net.",
    Postfix = ".dll",
});

//添加Assembly过滤(根据Assembly名称)
_iocCastle.AddFilter(new IocAssemblyNameFilter
{
    Prefix = "JCoder.Ioc4Net."
});

//添加Namespace过滤(根据Namespace名称)
_iocCastle.AddFilter(new IocNamespaceBunchFilter
{
    Prefix = "JCoder.Ioc4Net."
});

三、创建实例(依赖注入)

using JCoder.Ioc4Net;

public interface IServiceA { }
public class ServiceA : IServiceA { }

public class ServiceB
{
    public ServiceB(IServiceA a) { this.A = a; }
    public IServiceA A { get; }
}

IIocCastle _iocCastle = new IocCastle();
_iocCastle.RegisterAssembly(typeof(ServiceA).Assembly);

var _b = _iocCastle.Create<ServiceB>();   // 自动解析 IServiceA -> ServiceA
构造函数参数解析规则
参数类型 解析顺序
值类型 / string 预设值(按 类型 + 参数名 查找)→ 默认值
接口 / 类 标记 [NewObject] → 新实例;<br/>标记 [Singleton] → 单例;<br/>预设值 → 已注册单例 → 自动创建
循环依赖 自动检测并抛出 InvalidOperationException,避免栈溢出
使用特性控制依赖
public class ServiceC
{
    // 每次创建都使用新实例(即使其它地方已注册单例)
    public ServiceC([NewObject] IServiceA a) { ... }

    // 强制使用单例
    public ServiceC([Singleton] IServiceA a) { ... }
}

四、注册类型映射(手动注册)

IIocCastle _iocCastle = new IocCastle();

_iocCastle.Register<IServiceA>(typeof(ServiceA));        // 接口 -> 实现
_iocCastle.RegisterType<ServiceB>();                      // 按默认注册目标注册
_iocCastle.RegisterType(typeof(ServiceC)
    , IocRTarget.Interface | IocRTarget.Class);           // 指定注册目标

五、预设值(RegisterPrevalue)

IIocCastle _iocCastle = new IocCastle();

_iocCastle.RegisterPrevalue<int>("PageSize", 20);
_iocCastle.RegisterPrevalue<string>("ConnectionString", "server=.;database=db;");

// 创建实例时按“类型 + 参数名”自动注入
public class PageQuery
{
    public PageQuery(int pageSize) { ... }   // 自动注入 20
}

六、单例(Singleton)

IIocCastle _iocCastle = new IocCastle();

var _singleton = _iocCastle.GetSingleton<IServiceA>();   // 不存在时自动创建并缓存
_iocCastle.RegisterSingleton(new ServiceA());            // 手动注册单例

_iocCastle.UnregisterSingleton<IServiceA>();             // 注销单例

七、实现簇(Bunch,一个接口多个实现)

IIocCastle _iocCastle = new IocCastle();

_iocCastle.RegisterToBunchs(typeof(ServiceA));           // 注册该类型的所有基类/接口簇
_iocCastle.RegisterToBunch<IServiceA>(typeof(ServiceA)); // 注册到指定基类/接口

var _types = _iocCastle.GetBunchTypes<IServiceA>();      // 获取所有实现类型
var _items = _iocCastle.CreateFromBunch<IServiceA>();    // 创建所有实现实例

八、建造者(IocBuilder,流式注册)

using JCoder.Ioc4Net;

IIocCastle _iocCastle = new IocBuilder()
    .Register<ServiceA>()
        .AsInterfaces().AsSelf()                 // 注册接口映射 + 自身
    .Register(typeof(ServiceB))
        .As<IServiceB>()                         // 指定映射目标
    .Register(typeof(ServiceC).Assembly)
        .AsInterfaces()
        .Where(t => t.Namespace?.StartsWith("MyApp") == true)
    .Build();

注意:Register<TClass>() 之后需要调用 AsSelf() / AsInterfaces() / AsClass(deep) / As<TBase>() 之一,才会生成映射;AsClass(-1) 表示注册整个继承链(不含 object)。

九、事件(可选)

IIocCastle _iocCastle = new IocCastle();

_iocCastle.OnBeforeRegisterAssembly += (s, e) => { /* 注册程序集前 */ };
_iocCastle.OnTypeMapping          += (s, e) => { /* 类型映射后 */ };
_iocCastle.OnInstanceCreated      += (s, e) => { /* 实例创建后 */ };
_iocCastle.OnSingletonRegistered  += (s, e) => { /* 单例注册后 */ };

十、释放资源

_iocCastle.Dispose();   // 释放内部存储与事件订阅;之后再调用 API 会抛出 ObjectDisposedException

注意事项

  1. 类型以 AssemblyQualifiedName(失败时回退 FullName/Name)作为存储标识;
  2. RegisterAssembly 对已注册过的程序集保持幂等(返回 true,不重复注册);
  3. 程序集中存在接口、枚举、抽象类时不会导致注册失败(跳过并继续);
  4. 过滤器按 Priority 降序执行,任一过滤器返回 false 即被过滤。
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 was computed.  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 is compatible.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net10.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net8.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
3.2604.1 90 10/3/2026

Please contact Jackie by QQ 1378071853 or E-mail JackieLaw-dev@outlook.com;