本文介绍了如何在C#中创建字典属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
how to create properties variable that return Dictionary<string,string>.
我尝试了什么:
What I have tried:
private Dictionary<String, string> _Translation;
public Dictionary<String, string> Translations
{
get
{
return _Translation;
}
set
{
_Translation = GetTranslation();
}
}
public Dictionary<string, string> GetTranslation()
{
var translation = Translation.GetAll().ToList();
if (translation.Any())
{
foreach (var tran in translation)
{
Translations.Add(tran.Keys, tran.Value);
}
}
return _Translation;
}
public class Translation
{
public string Keys { get; set; }
public string Value { get; set; }
public static List<Translation> GetAll()
{
var result = new List<Translation>() {
new Translation() {Keys="btnSearch",Value="hello" }
};
return result;
}
}
推荐答案
public Dictionary<String, string> Translations
{
get
{
// Note this code is not thread-safe, you might need to add
// locking if it needs to be thread-safe.
if (_Translation == null)
{
_Translation = GetTranslation();
}
return _Translation;
}
// You could probably get away with the "set" altogether making Translations read only.
set
{
_Translation = value;
}
}
这篇关于如何在C#中创建字典属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!