我正在开发一个多语言网站。我基于以下 LocalizedDisplayAttribute 类显示多语言文本的解决方案。我想为这个类注入一些依赖。但是这个类是由.Net Framework实例化的。容器如何在不使用服务定位器的情况下注入依赖项?
这是我的代码块
[AttributeUsage(AttributeTargets.All)]
public class LocalizedDisplayAttribute : DisplayNameAttribute
{
public MultilanguageKeys Key { get; set; }
public IMultilanguageManager MultilanguageManager { get; set; }
public LocalizedDisplayAttribute(IMultilanguageManager multilanguageManager)
{
this.MultilanguageManager = multilanguageManager;
}
public override string DisplayName
{
get
{
// Run some business
return GetCustomAttribute(Key);
}
}
private string GetCustomAttribute(MultilanguageKeys key)
{
int multiLanguageId = 3;
MultiLanguageDictionaryInfo multiLanguageDictionary = MultilanguageManager.DictionaryList()
.Where(t => t.Key == key.ToString())
.Where(t => t.MultilanguageId == multiLanguageId)
.FirstOrDefault();
return multiLanguageDictionary.Value;
}
}