VContainer-scoping/generate-child-with-code-first | 作用域——通过代码优先生成子作用域
1.VContainer 介绍2.VContainer-about/what-is-vcontainer | 概述——VContainer 是什么?3.VContainer-about/what-is-di | 概述——DI 是什么?4.VContainer-getting-started/installation | 入门——安装5.VContainer-getting-started/hello-world | 入门——Hello World6.VContainer-resolving/constructor-injection | 解析——构造函数注入7.VContainer-resolving/method-injection | 解析——方法注入8.VContainer-resolving/property-field-injection | 解析——属性/字段注入9.VContainer-resolving/gameobject-injection | 解析——注入到 MonoBehaviours 中10.VContainer-resolving/container-api | 解析——容器 API11.VContainer-registering/register-type | 注册——注册纯 C# 类型12.VContainer-registering/register-using-delegate | 注册——注册使用委托13.VContainer-registering/register-factory | 注册——注册工厂14.VContainer-registering/register-monobehaviour | 注册——注册 MonoBehaviour15.VContainer-registering/register-scriptable-object | 注册——注册 ScriptableObject16.VContainer-registering/register-collection | 注册——注册集合17.VContainer-registering/register-callbacks | 注册——注册回调18.VContainer-integrations/entrypoint | 集成——纯 C# 入口点19.VContainer-integrations/unitask | 集成——Unitask20.VContainer-integrations/unirx | 集成——UniRx21.VContainer-integrations/messagepipe | 集成——MessagePipe22.VContainer-integrations/ecs | 集成——ECS (beta)23.VContainer-scoping/lifetime-overview | 作用域——生命周期管理总览24.VContainer-scoping/generate-child-via-scene | 作用域——通过场景或 prefab 生成子作用域
25.VContainer-scoping/generate-child-with-code-first | 作用域——通过代码优先生成子作用域
26.VContainer-scoping/project-root-lifetimescope | 作用域——项目根作用域 LifetimeScope27.VContainer-diagnostics/diagnostics-window | 诊断——VContainer 诊断窗口28.VContainer-optimization/source-generator | 优化——源码生成器29.**VContainer-optimization/async-container-build | 优化——异步容器构建30.VContainer-optimization/parallel-container-build | 优化——并行容器构建31.VContainer-optimization/codegen | 优化——预生成 IL 代码(已弃用)32.VContainer-comparing/comparing-to-zenject | 对比 Zenject33.MessagePipe 中文文档也可以通过 C# 代码直接创建子作用域。
class LevelLoader
{
readonly LifetimeScope currentScope;
LifetimeScope instantScope;
public LevelLoader(LifetimeScope lifetimeScope)
{
currentScope = lifetimeScope;
}
public void Load()
{
// ... 通过任意异步方式加载资源
//
// await Addressables.LoadAssetAsync...
//
// 为包含此 LevelLoader 实例的容器创建一个c子作用域。
instantScope = currentScope.CreateChild();
// 基于 LifetimeScope 预制体创建子作用域
instantScope = currentScope.CreateChildFromPrefab(
lifetimeScopePrefab);
// 基于 LifetimeScope 预制体创建并添加额外注册
instantScope = currentScope.CreateChildFromPrefab(
lifetimeScopePrefab, builder =>
{
builder.RegisterInstance(someExtraAsset);
builder.RegisterEntryPoint<ExtraEntryPoint>();
// ...
});
// 创建带额外注册的子作用域
instantScope = currentScope.CreateChild(builder =>
{
// ...
});
// 通过 `IInstaller` 创建子作用域并添加额外注册
instantScope = currentScope.CreateChild(extraInstaller);
// 额外注册的入口点会在作用域创建后立即运行...
// 或者直接使用作用域实例。
var foo = instantScope.Container.Resolve<Foo>();
}
public void Unload()
{
// 注意,作用域隐式创建了 `LifetimeScope`。
// 使用 `Dispose` 来安全销毁作用域。
instantScope.Dispose();
// ... 卸载相关资源
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)