我想避免以下的笨拙:
private void listBoxBeltPrinters_SelectedIndexChanged(object sender, System.EventArgs e)
{
string sel = string listBoxBeltPrinters.SelectedItem.ToString();
if (sel == "Zebra QL220")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ZebraQL220;
}
else if (sel == "ONiel")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ONiel;
}
else if ( . . .)
}
有没有一种方法可以根据列表框选择更优雅或更雄辩地分配给枚举,例如:
PrintUtils.printerChoice = listBoxBeltPrinters.SelectedItem.ToEnum(PrintUtils.BeltPrinterType)?
?