我的问题是,我希望使用Func<>工厂来解决依赖。如果我使用ContainerBuilder Update() (我需要它来模拟集成测试中的某些服务),这个工厂仍然可以解决过时的实例。
我创建了一个简单的场景来重现问题:
class Program
{
static void Main(string[] args)
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<Test>().As<ITest>();
我想弄清楚如何让DryIoc在ExampleClass上解决ITest问题?
这是将参数名称与服务键匹配,因为有多个注册来定位正确的服务。
public class Program
{
public void Main()
{
var container = new Container();
container.Register<ITest, A>(serviceKey: "a");
container.Register&l
我得到了一个很小的接口ITest:
public interface ITest
{
void DoSomething();
}
以及ITest的一些实现
public class Test : ITest
{
public void DoSomething()
{
throw new NotImplementedException();
}
}
public class Test2 : ITest
{
public void DoSomething()
{
throw new NotImplementedExc
最近,我在WPF项目中使用了Prism.DryIoC库。我在App.xaml.cs类中发现重写RegisterTypes方法
using Prism.Ioc;
using WpfAppNetCore.Views;
using System.Windows;
namespace WpfAppNetCore
{
public partial class App
{
protected override Window CreateShell()
{
return Container.Resolve<MainWindow&
我有实现ITest的接口ITest和类测试:
public Test : ITest{...}
我有这样的课程:
public class MyClass
{
ITest test;
public MyClass(){...}
}
有没有办法用Ninjection在test变量中设置新的Test()示例?我想在构造函数中去掉下面的代码:
public MyClass()
{
test = new Test();
}
谢谢!
是否可以在派生类中运行方法而不让基类知道它的名称。IE :找出实现ITest的人,并运行该类中指定的方法。
我知道奇怪的问题。
class BaseClass
{
public static void Main (string[] args)
{
ITest f; //Null here. Want this to auto magically look for classes implementing ITest.
f.Run (); //And then run this. #NoReflection #NoBlackMagic.
假设我们有一个有两个构造函数的类,第一个是空的(默认),第二个是构造函数参数。
class Test : ITest {
Public Test () {}
public Test (string parameter) { do something; }
在NInject中,它可以使用:
Bind<ITest>().To<Test>();
Bind<ITest>().To<Test>()
.When(ctx => IsTrue())
.WithConstructorArg
我希望将实现公共接口的所有实例作为IEnumerable<T>注入。它用于策略模式解析器中。我该怎么做呢?我在谷歌上搜索了统一容器有一种方法。
我需要这个做同样的工作,与下面的链接。
public interface ITest
{
string Name { get; set; }
}
public class TestA : ITest
{
public string Name { get; set; } = "A";
}
public class TestB : IT
我们使用统一依赖注入。我们有一个类需要传递给它的构造函数的类型。
public interface ITest
{
}
public class Test : ITest
{
public Test(Type myType)
{
}
}
在容器助推器中,我们注册我们的类型:
public static IUnityContainer Start()
{
IUnityContainer container = UnityInversionOfControlContainer.RegisterContainer();
container.Registe
我有这样一门课:
public class Test : ITest
{
public Test(IOptions<CustomSection> settings)
{
//do something
}
}
我想要单元测试和维护依赖注入。为了做到这一点,我尝试了以下几点:
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.AddEnvironmentVariables();
var co
我遇到了一个问题,在使用城堡、温莎和拦截器时,我的程序会耗尽内存。使用以下代码是可以复制的:
public interface ITest{}
public class Test : ITest {}
class TestInterceptor:IInterceptor {
public void Intercept(IInvocation invocation) {}
}
class Program {
static void Main(string[] args) {
while(true) {
using(var container
我试图在不知道实际数字的情况下找到正确的方法来设置For中的字符串值。下面是我试图做的事情,因为在vb6中是可能的,但不确定使用vb.net
Public Class Form1
Dim iTest1 As String
Dim iTest2 As String
Dim iTest3 As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To 3
"iTest" & i = "aaa
我正在尝试注册一个接口的不同实现,并取决于使用这些实现的类,特定的实现将被传递。
public interface ITest { }
public class Test1 : ITest { }
public class Test2 : ITest { }
public class DoSmthWhichCurrentlyNeedsTest1
{
private ITest test;
public DoSmthWhichCurrentlyNeedsTest1(ITest test)
{
this.test = test;
}
}
pu
我有一个带有反变型参数的接口,比如IFoo
interface IFoo<in T> {}
现在我有三门课:
class B {}
class D : B {}
class Foo : IFoo<B> {}
如果我像这样注册他们
container.RegisterType<IFoo<B>, Foo>();
...then试图解决IFoo<D>,因为我没有真正注册IFoo<D>或IFoo<>,所以失败了。清楚了。
我的当前解决方案只是在Registrations上迭代,找到RegisteredType可以从
如果我们有以下示例应用程序:
interface ITest
{
string Test { get; }
}
class A : ITest
{
string ITest.Test { get { return "Test from A!"; } }
}
class B : A, ITest
{
string ITest.Test { get { return "Test from B!"; } }
}
给定B的一个实例,是否可以访问A的ITest实现?例如:
B b = new B();
ITest test = b;
strin
我试图用一个名为x的变量定义一个类,它的类型与一些通用的T匹配。这很容易实现:
public class Test<T>{
public T x{get; set;}
}
问题在于,我希望能够同时讨论具有不同T的T实例,例如在声明具有类型为Test<T>、Test<S>等类型的元素的List<Test>时。下面是我尝试的:
public interface ITest{
object x {get; set;}
}
public interface ITest<T> : ITest{
T x {get; se
我需要创建一个dll文件,它包含类的所有接口,但不包含任何类。因为我将这些接口用于很多类,所以它必须是这样的:
public interface IClassA
{
string Word { get; }
}
public interface ITest<TClassA> where TClassA : IClassA
{
TClassA A { get; }
}
以我想要的方式实现这些接口的两个类示例:
public class ClassA : IClassA
{
public string Word
{
get;