首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用Ninject获取对象的实例

使用Ninject获取对象的实例是指在使用Ninject依赖注入框架时,如何创建和获取对象的实例。Ninject是一个开源的依赖注入框架,它可以帮助开发者更轻松地实现依赖注入,并且可以在不同的环境中使用。

在Ninject中,可以使用以下方法获取对象的实例:

  1. 使用ToMethod方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().ToMethod(context => new MyImplementation());
  1. 使用ToConstant方法注册对象实例:
代码语言:csharp
复制
IMyInterface myInstance = new MyImplementation();
kernel.Bind<IMyInterface>().ToConstant(myInstance);
  1. 使用ToProvider方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().ToProvider<MyProvider>();
  1. 使用ToSelf方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<MyImplementation>().ToSelf();
  1. 使用InSingletonScope方法注册单例对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().InSingletonScope();
  1. 使用InTransientScope方法注册瞬态对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().InTransientScope();
  1. 使用InRequestScope方法注册请求范围对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().InRequestScope();
  1. 使用InThreadScope方法注册线程范围对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().InThreadScope();
  1. 使用InNamedScope方法注册命名范围对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().InNamedScope("myScope");
  1. 使用WhenInjectedInto方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WhenInjectedInto<MyConsumer>();
  1. 使用WithConstructorArgument方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgument("arg1", "value1");
  1. 使用WithPropertyValue方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValue("PropertyName", "PropertyValue");
  1. 使用WithParameters方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().ToMethod(context => new MyImplementation(context.Parameters.First()));
  1. 使用WithCircularDependencies方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithCircularDependencies();
  1. 使用OnActivation方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().OnActivation(instance => instance.Initialize());
  1. 使用OnDeactivation方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().OnDeactivation(instance => instance.Dispose());
  1. 使用When方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().When(request => request.Target != null && request.Target.Member.Name == "MyMethod");
  1. 使用WithMetadata方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithMetadata("key", "value");
  1. 使用WithAnyAttribute方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithAnyAttribute();
  1. 使用WithAttribute方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithAttribute(new MyAttribute());
  1. 使用WithAttributes方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithAttributes(new MyAttribute(), new MyAttribute2());
  1. 使用WithKernel方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithKernel(new MyKernel());
  1. 使用WithParentScope方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParentScope();
  1. 使用WithScope方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithScope(new MyScope());
  1. 使用WithUniqueId方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithUniqueId("myUniqueId");
  1. 使用WithConstructorArgumentFromContext方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromContext("arg1", context => context.Parameters.First());
  1. 使用WithPropertyValueFromContext方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromContext("PropertyName", context => context.Parameters.First());
  1. 使用WithParametersFromContext方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromContext(context => new ConstructorArgument("arg1", context.Parameters.First()));
  1. 使用WithConstructorArgumentFromMethod方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromMethod("arg1", context => context.Parameters.First());
  1. 使用WithPropertyValueFromMethod方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromMethod("PropertyName", context => context.Parameters.First());
  1. 使用WithParametersFromMethod方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromMethod(context => new ConstructorArgument("arg1", context.Parameters.First()));
  1. 使用WithConstructorArgumentFromProvider方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromProvider("arg1", context => new MyProvider());
  1. 使用WithPropertyValueFromProvider方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromProvider("PropertyName", context => new MyProvider());
  1. 使用WithParametersFromProvider方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromProvider(context => new MyProvider());
  1. 使用WithConstructorArgumentFromResolutionRoot方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromResolutionRoot("arg1", context => context.Kernel.Get<MyService>());
  1. 使用WithPropertyValueFromResolutionRoot方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromResolutionRoot("PropertyName", context => context.Kernel.Get<MyService>());
  1. 使用WithParametersFromResolutionRoot方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromResolutionRoot(context => new ConstructorArgument("arg1", context.Kernel.Get<MyService>()));
  1. 使用WithConstructorArgumentFromService方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromService("arg1", context => context.Kernel.Get<MyService>());
  1. 使用WithPropertyValueFromService方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromService("PropertyName", context => context.Kernel.Get<MyService>());
  1. 使用WithParametersFromService方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromService(context => new ConstructorArgument("arg1", context.Kernel.Get<MyService>()));
  1. 使用WithConstructorArgumentFromSyntax方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromSyntax("arg1", context => context.Kernel.Get<MyService>());
  1. 使用WithPropertyValueFromSyntax方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromSyntax("PropertyName", context => context.Kernel.Get<MyService>());
  1. 使用WithParametersFromSyntax方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromSyntax(context => new ConstructorArgument("arg1", context.Kernel.Get<MyService>()));
  1. 使用WithConstructorArgumentFromType方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromType("arg1", typeof(MyService));
  1. 使用WithPropertyValueFromType方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromType("PropertyName", typeof(MyService));
  1. 使用WithParametersFromType方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromType(typeof(MyService));
  1. 使用WithConstructorArgumentFromValue方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromValue("arg1", "value1");
  1. 使用WithPropertyValueFromValue方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromValue("PropertyName", "value1");
  1. 使用WithParametersFromValue方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromValue(new ConstructorArgument("arg1", "value1"));
  1. 使用WithConstructorArgumentFromValues方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromValues("arg1", new[] { "value1", "value2" });
  1. 使用WithPropertyValueFromValues方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromValues("PropertyName", new[] { "value1", "value2" });
  1. 使用WithParametersFromValues方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromValues(new ConstructorArgument("arg1", new[] { "value1", "value2" }));
  1. 使用WithConstructorArgumentFromName方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromName("arg1", "name1");
  1. 使用WithPropertyValueFromName方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromName("PropertyName", "name1");
  1. 使用WithParametersFromName方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromName(new ConstructorArgument("arg1", "name1"));
  1. 使用WithConstructorArgumentFromTypeOf方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromTypeOf("arg1", typeof(MyService));
  1. 使用WithPropertyValueFromTypeOf方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithPropertyValueFromTypeOf("PropertyName", typeof(MyService));
  1. 使用WithParametersFromTypeOf方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithParametersFromTypeOf(typeof(MyService));
  1. 使用WithConstructorArgumentFromTypeOfName方法注册对象实例:
代码语言:csharp
复制
kernel.Bind<IMyInterface>().To<MyImplementation>().WithConstructorArgumentFromTypeOfName("arg1", typeof(MyService), "name1");
  1. 使用WithPropertyValueFromTypeOfName方法注册对象实例:
代码语言:csharp
复制

kernel

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

如何使用 ref 属性获取子组件实例对象

在 Vue 中,我们可以使用 ref 属性来获取子组件实例对象。这个功能非常方便,可以让父组件直接访问子组件方法和数据。本文将详细介绍如何使用 ref 属性获取子组件实例对象。...当使用 ref 属性时,Vue 将会创建一个 $refs 对象,并将注册了 ref 元素或组件引用存储到 $refs 对象中。这个 $refs 对象可以很方便地用来访问子组件实例对象。...在父组件中通过 ref 获取子组件实例对象在父组件中,我们可以通过 ref 属性获取子组件实例对象。...在子组件中通过 $parent 访问父组件实例对象除了在父组件中获取子组件实例对象以外,我们也可以在子组件中通过 $parent 访问父组件实例对象。...需要注意是,在子组件中使用 $parent 访问父组件实例对象需要慎重使用,因为它会使组件之间耦合度变高,不利于组件复用和维护。

2.5K00

使用 Clientset 获取 Kubernetes 资源对象

本节主要讲解 Kubernetes 核心资源类型 Scheme 定义以及如何使用 Clientset 来获取集群资源对象。...示例 首先我们来看下如何通过 Clientset 来获取资源对象,我们这里来创建一个 Clientset 对象,然后通过该对象获取默认命名空间之下 Deployments 列表,代码如下所示: package...Kubernetes 集群资源方式,通过 client-go 提供 Clientset 对象获取资源数据,主要有以下三个步骤: 使用 kubeconfig 文件或者 ServiceAccount...操作 Clientset 对象 上面我们了解了如何使用 Clientset 对象获取集群资源,接下来我们来分析下 Clientset 对象实现。...Informers 第一次被调用时候会首先在客户端调用 List 来获取全量对象集合,然后通过 Watch 来获取增量对象更新缓存,这个我们后续在讲解。

4K10

通过 Request 对象实例获取用户请求数据

注入请求对象 在 Laravel 中,访问用户输入数据最常用方式,就是通过注入到控制器方法中 Illuminate\Http\Request 对象实例,通过该实例,我们可以访问所有用户请求数据,不管是什么方式...Request 门面和全局辅助函数 request(),本质都是调用 Illuminate\Http\Request 对象实例,只不过形式不一样而已,所以我一般就是用 Request 对象实例。...接下来,我们就可以在控制器方法中通过 $request 对象实例获取用户请求了,Request 类提供了多种方法来访问用户请求数据。...只获取部分请求数据 如果只想获取部分请求数据,可以通过 $request 实例 except 或 only 方法,这两个方法是相反,一个用于排除指定字段,一个用于获取指定字段: $request-...作为定义路由匿名函数参数之间已经见过很多了,这里我们来演示下作为控制器方法参数传入,以及如何通过 Request 对象实例获取

19.7K30

使用Unity获取所有子对象及拓展方法使用

一、前言 这个问题还是比较简单,无非就是一个for循环就可以全部获取到了,但是我喜欢简单直达,有没有直接就能获取到所有的子对象函数呢,搜了好久都没有,所以我准备写一个扩展函数,来自己补充这个函数,一起来看一下吧...二、如何获取所有子对象 第一种方法: 使用foreach循环,找到transform下所有的子物体 foreach(Transform child in transform) { Debug.Log...三、使用扩展方法获取所有子对象 总感觉获取个子对象还要用for循环有点麻烦,那么咱们就可以写一个扩展方法,直接获取到所有的子对象 1、首先新建一个MyExtensions.cs脚本 using System.Collections.Generic...i] = obj.transform.GetChild(i).gameObject; } return tempArrayobj; } } 这有两个函数,一个是获取所有子对象...List集合,一个是获取所有子对象数组集合,按需使用

2.4K30

JS获取事件对象获取事件对象(Firefox,IE)

做笔记,以防自己忘记~~ JS获取事件event,不同浏览器有不同做法。 例如IE下,在js函数中,通过window.event就可以获取,不必在函数中添加什么参数。...也可以用Prototype或者JQuery等,它们有他们对事件包装。还是使用JS库比较好,不然就有下边麻烦。...注意获取标记都以大写表示,如"TD","TR","A"等。所以把看过一些抄下来,不记得时候再来看看。...下 event.target = IE 下 event.srcElement 解决方法:使用obj(obj = event.srcElement ?...在 ie中处理事件直接使用window.event对象即可,但在firefox中,是没有 window.event对象,函数需要使用事件时候,需要在事件发生时把事件作为参数传递给函数,不象在ie

10K50

IoC原理-使用反射Emit来实现一个最简单IoC容器

这里是仿造Ninject配置方式,使用代码来配置。这种配置方式有个好处就是不会写错,因为有IDE来给你检查拼写错误。...不要小看这个好处,当你有上百个注入对象时候,使用UnityXML来配置对应关系时候很容易就会发生拼写错误。这种错误往往还很难发现。...Ninject实现方式。...这里手动使用IoC容器去获取对应实例对象,我们也可以配合特性来使代码更加简单。这里就不实现了。 8.总结 通过这么短短几行代码。我们实现了一个最最简单IoC容器。...但是这就已经揭示了IoC框架最本质东西:反射或者EMIT来实例对象。然后我们可以加上缓存,或者一些策略来控制对象生命周期,比如是否是单例对象还是每次都生成一个新对象。  源码

823100

获取Object对象length

所有JS程序猿(甚至不止JS)都知道,数组(Array)是有length,通过length属性,可以很方便获取数组长度。可以说,只要使用到了数组,就必会使用到其length属性。...而Object对象是没有length属性或方法,它确实没有存在必要,因为人们只会在乎该对象能提供什么样方法,而没有必要知道它到底有多少方法。...的确,这确实不是一个普遍性需求, 因此ECMAScript中也不会为自己增加额外负担。 我之前一直没有考虑过这个问题,我们通过CGI获取数据,对于一条一条数据,后台将其做成数组并以json返回。...}); 4 }catch(e){} 面对这样数据,我就犯愁了,因为object不能获取对象长度。...其实要获取对象长度也不难,用for in 语句就能实现,如下代码所示: var a = {a:1,b:2,c:3,d:4}; function length(obj) { var count

2.2K110
领券