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

时间:2018-09-10 20:42:34

标签: c# json .net-core

当我是新手和学习者时,请原谅我的无知/错误。我有一个静态类,其国家名称为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";
        }
    }
}

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

我从StackOverflow的答案之一中看到了Type.GetType()方法,但是由于我没有创建实例,所以我不知道如何在这里使用该方法。请提供解决方案的示例,以使其更易于理解。谢谢。

4 个答案:

答案 0 :(得分:8)

您不需要24个不同的类,您需要1个具有24个实例的类。

public class Country
{
    public int ID { get; }
    public string NameOfCountry { get; }
    public string Abbreviation { get; }
    public string FlagLocation { get; }

    public Country(int id, string nameOfCountry, string abbreviation, string flagLocation)
    {
        ID = id;
        NameOfCountry = nameOfCountry;
        Abbreviation = abbreviation;
        FlagLocation = flagLocation;
    }        
}

请注意,如果问题中这些属性为static,则所有实例将共享该值,这是您在此处不需要的。

存储这些类的最佳方法(假设您无法使用数据库)是使用字典:

private static Dictionary<string, Country> _countries = new Dictionary<string, Country>
{
    ["Finland"] = new Country(7, "Finland", "FI", "finishFlag.png"),
    ["USA"] = ...
};

然后您可以按其名称访问这些国家/地区:

Country country = _countries["Finland"];

将国家/地区添加到字典中比创建新类和添加新if例要容易得多。

答案 1 :(得分:3)

不要创建一堆静态类。而是创建一个Country类,然后创建每个国家的对象。

public class Country
{
    public int ID { get; set; }
    public string Name { get; set; }
    public tring Abbreviation { get; set; }
    public tring FlagLocation { get; set; }
}

那么您可以拥有一个静态词典

public static Dictionary<string, Country> Countries = 
{
    ["Finland"] = new Country
    {
        ID = 7,
        Name = "Finland",
        Abbreviation = "FI",
        FlagLocation = "finishFlag.png"
    },
    ["Germany"] = new Country
    {
        ID = 8,
        Name = "Germany",
        Abbreviation = "DE",
        FlagLocation = "germanFlag.png"
    }
}

然后给定一个国家的名称,您可以像这样获得船旗位置

Countries["Finland"].FlagLocation;

答案 2 :(得分:1)

如果仅使用静态类,则不能依赖类之间的任何公共结构(静态类不能参与多态)。

相反,如果您使用界面定义国家/地区,并且在代码中的某处初始化了每种国家/地区类型的单例实例,该怎么办?然后,当您返回一个字符串时,可以搜索具有正确国家名称的实例,并使用您认为合适的其余信息。

作为@juharr答复的替代方法,您可以使用一个接口,然后让每个国家/地区将其作为专用类来实现;如果您发现自己需要这种行为,则可以使用特定国家/地区的行为。如果您不这样做,那么@juharr的答案是有效的。

public interface ICountry
{
    int Id { get; }

    string Name { get; }

    // .. and so on.
}

public class Finland : ICountry
{
    public string Name { get; private set; } = "Finland";

    public int Id { get; private set; } = 7;
}

public class CountryRegistry
{
    private Dictionary<string, ICountry> countryMap;

    public CountryRegistry()
    {
        this.countryMap = new Dictionary<string, ICountry>();

        InitCountries();
    }

    public ICountry FindByName( string searchName )
    {
        ICountry result;
        if( this.countryMap.TryGetValue( searchName, out result ) )
        {
            return result;
        }
        else
        {
            return null;
        }
    }

    private void InitCountries()
    {
        AddCountryToMap( new Finland() );
        // .. and so on
    }

    private void AddCountryToMap( ICountry country )
    {
        this.countryMap.Add( country.Name, country );
    }
}

答案 3 :(得分:1)

将对象声明为类在程序员的世界中并不常见。类是定义模板(和行为集合)的地方。您可以通过以下方式定义国家/地区类别:

public class Country    
{
    public int ID { get; set; };
    public string NameOfCountry { get; set; };
    public string Abbreviation { get; set; };
    public string FlagLocation { get; set; };
}

,然后将您的国家/地区定义为静态对象:

 static Country Finland = new Country() { ID = 7, 
                                          NameOfCountry="Finland",
                                          Abbreviation  = "FI",
                                          FlagLocation = "finishFlag.png"
                                         };
相关问题