检查字符串是否可以转换为其他类型(C#)

Add*_*ery 4 c# enums types

我正在用C#做一些测试,我现在需要知道一些事情.我创建了一个类,像这样:

class DChanger
{
    //Just ignore this:
    private string section = Csuc.CPanel.CPanelSection.Appearance;
    //Then the constructor: DChanger dchange = new DChanger(Internet);
    public void DChanger(string subsection)
    {
        //Code
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,现在.我想检查"subsection"是否可以转换为其他类型.在示例中,我有一个枚举:

enum Subsections { Internet, Programming };
Run Code Online (Sandbox Code Playgroud)

我想检查"subsection"是"Internet"还是"Programming"(在这个例子中,因为真正的enum有更多的部分).我能这样做吗?谢谢!

lfb*_*ger 10

您可以使用Enum.Parse并注意ArgumentException类型的异常.您也可以使用Enum.TryParse.

更多信息请访问http://msdn.microsoft.com/en-us/library/essfb559.aspx