2

我想替换这段代码:

Kernel.Bind<ITaskFactory<AlertTask>>().ToFactory();
Kernel.Bind<ITaskFactory<PopupTask>>().ToFactory();

像这样:

Kernel.Bind(scanner => scanner.FromThisAssembly().SelectAllClasses()
   .EndingWith("Task").MAGICGOESHERE().BindToFactory()

我尝试创建一个 IBindingGenerator,但在尝试从 T 获取 ITaskFactory<T> 时陷入困境。

4

1 回答 1

6

改用这个:

kernel.Bind(scanner => scanner
    .FromThisAssembly()
    .SelectAllInterfaces()
    .EndingWith("Factory")
    .BindToFactory());

kernel.Bind(scanner => scanner
    .FromThisAssembly()
    .SelectAllClasses()
    .EndingWith("Task"));

var f = kernel.Get<ITaskFactory>();
var task1 = f.CreateTask<AlertTask>();
var task2 = f.CreateTask<PopupTask>();

public class AlertTask {}
public class PopupTask { }
public interface ITaskFactory
{
    T CreateTask<T>();
}
于 2013-03-04T17:55:07.500 回答