【发布时间】:2013-02-05 04:36:20
【问题描述】:
我有泛型 Result<T> 泛型类,我经常在方法中使用它来返回这样的结果
public Result<User> ValidateUser(string email, string password)
Result 类中有 ILoggingService 接口用于记录服务注入,但我没有找到注入实际实现的方法。
我尝试执行下面的代码,但 TestLoggingService 实例未注入到 LoggingService 属性中。它总是返回 null。任何想法如何解决它?
using (var kernel = new StandardKernel())
{
kernel.Bind<ILoggingService>().To<TestLoggingService>();
var resultClass = new ResultClass();
var exception = new Exception("Test exception");
var testResult = new Result<ResultClass>(exception, "Testing exception", true);
}
public class Result<T>
{
[Inject]
public ILoggingService LoggingService{ private get; set; } //Always get null
protected T result = default(T);
//Code skipped
private void WriteToLog(string messageToLog, object resultToLog, Exception exceptionToLog)
{
LoggingService.Log(....); //Exception here, reference is null
}
【问题讨论】:
标签: c#-4.0 dependency-injection ninject