C#技巧 – 通过string获取常量值

有没有什么好的方法来将string“xlSum”,“xlAverage”和“xlCount”转换为Microsoft.Office.Interop.Excel.XlConsolidationFunction下的值?

我想reflection会很慢(如果可能的话)。 这些常数值大约有10个。 如果可能的话,我试图避免一个大的switch语句。

这是一个枚举,所以你应该能够使用

using Microsoft.Office.Interop.Excel; XlConslidationFunction func = (XlConsolidationFunction) Enum.Parse( typeof(XlConsolidationFunction), stringVal ); 

您可以始终使用Dictionary<string, ...>来替代开关Dictionary<string, ...>并在应用程序启动时填充一次