我使用文件夹中的批量添加在容器中注册了几种类型(界面及其具体类型)(下面是代码)。当我尝试检索实例时,我无法获取实例。
var pluginDLLs = new DirectoryInfo(pluginDirectory).GetFiles()
.Where(file => file.Extension.ToLower() == ".dll")
.ToList();
var pluginAssembliesList =
pluginDLLs.Select(dll => Assembly.LoadFrom(dll.FullName)).ToList();
var pluginAssemblies = pluginAssembliesList
.Where(pluginAssembly => pluginAssembly.FullName.StartsWith("QS."))
.ToList();
pluginAssembliesTypes =
pluginAssemblies.SelectMany(a => a.GetExportedTypes()).ToList();
//Identify commands based on ICommand. Code that identifies all command including ICommandGetTestResultsRun
var icommandType = pluginAssembliesTypes
.First(type => type.IsInterface && type.Name.Equals("ICommand"));
var commands = (
from type in pluginAssembliesTypes
where !type.IsInterface
where icommandType.IsAssignableFrom(type)
where type.GetInterfaces().Any(t => type.GetConstructor(new Type[] { t }) != null)
select new
{
Service = type.GetInterfaces()
.First(t => type.GetConstructor(new Type[] { t }) != null),
Implementation = type
}).ToList();
commands.ForEach(reg =>
container.Register(reg.Service, reg.Implementation, Lifestyle.Transient));
var test = container.GetInstance(typeof (ICommandGetTestResultRuns));
我附上Simple Injector源代码的屏幕截图,我认为它失败了。你能不能找出我做得不对的事。
这是ICommandGetTestResultRuns实现。
namespace QS.Rand.BusinessLogic.Commands
{
public interface ICommand
{
}
}
namespace QS.Rand.BusinessLogic.Commands.TestResults
{
public interface ICommandGetTestResultRuns : ICommand
{
IntFilter IdTestRequestFilter { get; set; }
IntFilter IdSampleFilter { get; set; }
IntFilter IdBatchFilter { get; set; }
IntFilter IdFilter { get; set; }
}
}
namespace QS.Rand.BusinessLogic.Commands
{
public class CommandGetTestResultRuns : Command<IEnumerable<ITestResultRun>>, ICommandGetTestResultRuns
{
protected static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(CommandGetTestResultRuns));
#region ICommandGetTestRuns implementation
public IntFilter IdTestRequestFilter { get; set; }
public IntFilter IdSampleFilter { get; set; }
public IntFilter IdBatchFilter { get; set; }
public IntFilter IdFilter { get; set; }
#endregion
protected DA.ITestRunRepository TestRunRepository { get; set; }
public CommandGetTestResultRuns() { }
public CommandGetTestResultRuns(ICommandGetTestResultRuns cmd)
{
IdTestRequestFilter = cmd.IdTestRequestFilter;
IdFilter = cmd.IdFilter;
IdSampleFilter = cmd.IdSampleFilter;
IdBatchFilter = cmd.IdBatchFilter;
}
public override IResult<IEnumerable<ITestResultRun>> Execute()
{
var result = new Result<IEnumerable<ITestResultRun>>();
var filter = new DaTestRunFilter();
try
{
var daRuns = TestRunRepository.GetTestRuns(filter, new List<Tuple<string, DA.OrderDirection>>());
var runs = new List<ITestResultRun>();
result.Value = runs;
result.IsOK = true;
}
catch (Exception ex)
{
result = new Result<IEnumerable<ITestResultRun>>() { IsOK = false, Message = new ResultMessage() { Level = MessageLevel.Error, Text = ex.Message }, Value = null };
logger.Error(ToLogString(), ex);
}
return result;
}
private string ToLogString()
{
var sb = new StringBuilder();
sb.AppendFormat("Unable to load test request samples");
return sb.ToString();
}
}
}