C#技术 - 按字符串获取常量值

时间:2021-12-31 16:48:25

Is there any good way to convert strings like "xlSum", "xlAverage", and "xlCount" into the value they have under Microsoft.Office.Interop.Excel.XlConsolidationFunction?

有没有什么好方法可以将像“xlSum”,“xlAverage”和“xlCount”这样的字符串转换为它们在Microsoft.Office.Interop.Excel.XlConsolidationFunction下的值?

I guess reflection would be slow (if its possible). There are about 10 of these constant values. I was trying to avoid a large switch statement if possible.

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

2 个解决方案

#1


5  

This is an enum so you should be able to use

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

using Microsoft.Office.Interop.Excel;

XlConslidationFunction func = (XlConsolidationFunction)
                               Enum.Parse( typeof(XlConsolidationFunction),
                                           stringVal );

#2


0  

Instead of switch you can always use a Dictionary<string, ...> and fill it once when the application starts

您可以始终使用Dictionary 而不是切换,并在应用程序启动时填充一次 ,...>

#1


5  

This is an enum so you should be able to use

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

using Microsoft.Office.Interop.Excel;

XlConslidationFunction func = (XlConsolidationFunction)
                               Enum.Parse( typeof(XlConsolidationFunction),
                                           stringVal );

#2


0  

Instead of switch you can always use a Dictionary<string, ...> and fill it once when the application starts

您可以始终使用Dictionary 而不是切换,并在应用程序启动时填充一次 ,...>