小编cpt*_*niz的帖子

如何从字符串中调用静态类方法?

请原谅我的无知/错误,因为我是新手和学习者.我有一个带有国家名称的静态类,Finland如下所示.

namespace Countries
{
    public static class Finland
    {
        public static int ID { get; set; } = 7;
        public static string nameOfCountry { get; set; } = "Finland";
        public static string abbreviation { get; set; } = "FI";
        public static string flagLocation { get; set; } = "finishFlag.png";
        public static string GetFlag()
        {
            return "finishFlag.png";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用HttpClient从网站上执行GET请求JSON字符串.然后我使用DeserializeObject将JSON反序列化为一个对象.对象的一个​​变量是string countryName(与字符串nameOfCountry完全匹配).通过使用此字符串(countryName),我想GetFlag()从相应的国家/地区类调用该方法.但是,我不知道如何从字符串中调用此静态方法countryName.我可以将nameOfCountry字符串与countryName字符串进行比较,但是我有24个国家类,如类Finland,这意味着24 if else if语句.

Type.GetType() …

c# json .net-core

0
推荐指数
1
解决办法
150
查看次数

标签 统计

.net-core ×1

c# ×1

json ×1