我的程序会找到您所在国家/地区的IP地址。 当你来自德国时,它会显示DE 当你在美国时,它会显示美国 等
现在我的问题是如何自动将琴弦从HU转到匈牙利。 我应该创建一个项目列表还是那样的?
你看,我从网站http://api.hostip.info/country.php获取国家/地区信息 您可以看到上面的获取IP按钮国家:DE,我想在德国自动更改
答案 0 :(得分:1)
您可以创建字典并将密钥设置为返回的结果,其值为所需的国家/地区名称。在此处查看基本概述:Tutorial
答案 1 :(得分:1)
以下是解决此问题的两个选项:
Dictionary<Country, string>
Country
枚举并使用Description
属性构建Dictionary<Country, string>
,如下所示:
enum Country
{
UnitedStates,
Germany,
Hungary
}
Dictionary<Country, string> CountryNames = new Dictionary<Country, string>
{
{ Country.UnitedStates, "US" },
{ Country.Germany, "DE" }
{ Country.Hungary, "HU" }
};
static string ConvertCountry(Country country)
{
string name;
return (CountryNames.TryGetValue(country, out name))
? name : country.ToString();
}
现在您可以通过静态Dictionary<Country, string>
方法使用ConvertCountry
,如下所示:
var myCountry = ConvertCountry(Country.UnitedStates));
使用Country
枚举并使用Description
属性进行装饰,如下所示:
enum Country
{
[Description("US")]
UnitedStates,
[Description("DE")]
Germany,
[Description("HU")]
Hungary
}
现在,您可以使用此方法获取Description
属性值,如下所示:
public static string GetDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return en.ToString();
}
使用上述方法,如下:
var myCountryDescription = GetDescription(Country.UnitedStates);
答案 2 :(得分:1)