开始我正在使用Ninject 1.5.我有两个项目:Web项目和类库.我的DI配置在Web项目中.在我的类库中,我定义了以下内容:
public interface ICacheService<T>
{
string Identifier { get; }
T Get();
void Set( T objectToCache, TimeSpan timeSpan );
bool Exists();
}
Run Code Online (Sandbox Code Playgroud)
然后是一个名为CategoryCacheService的具体类.
在我的web项目中,我绑定了两个:
Bind( typeof( ICacheService<List<Category>> ) ).To( typeof(CategoryCacheService)).Using<SingletonBehavior>();
Run Code Online (Sandbox Code Playgroud)
在我的类库中,我有HtmlHelper类的扩展方法,例如:
public static class Category
{
[Inject]
public static ICacheService<List<Category>> Categories { get; set; }
public static string RenderCategories(this HtmlHelper htmlHelper)
{
var c = Categories.Get();
return string.Join(", ", c.Select(s => s.Name).ToArray());
}
}
Run Code Online (Sandbox Code Playgroud)
我被告知你不能注入静态属性,而是我应该使用Kernel.Get <>() - 但是......由于上面的代码在类库中,我无法访问内核.如何从这一点获取内核,或者有更好的方法吗?