有关在 C# 中构建错误代码查找的建议
我是 C# 新手,如果我的一些概念有偏差,请原谅我。我正在使用 Canon EDSDK,为了让生活更轻松,我希望能够将错误消息视为文本而不是十六进制值。 EDSDK.cs 文件包含一长串错误,例如:
public const uint EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07;
public const uint EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08;
理想情况下,我想将所有这些放入某种查找表中,以便我可以输入十六进制错误代码并以字符串形式返回实际消息。例如
Hashtable EDSDKErrorCodes = new Hashtable();
EDSDKErrorCodes.Add("0x00008D01", "EDS_ERR_TAKE_PICTURE_AF_NG");
EDSDKErrorCodes.Add("0x00008D08", "EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG");
etc etc...
,在我的代码中,我可以抓取从 EDSDK 方法之一返回的错误消息,并以人类可读的形式显示它:
errorcode= EDSDK.EdsInitializeSDK();
MessageBox.Show(Convert.ToString(EDSDKErrorCodes[errorcode]));
我的问题是..这是处理所有这些错误代码的好方法还是有更好的方法人们会推荐更有效的方式吗?如果是这样,我如何将哈希表填充为一系列常量,而不必使用 Add 方法?
I'm new to C# and so forgive me if I have some of my concepts skewed. I'm working with the Canon EDSDK, and to make life easier I'd like to be able to see error messages as text rather than hex values. The EDSDK.cs file contains a long list of errors such as :
public const uint EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07;
public const uint EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08;
Ideally I'd like to put all of these into some sort of lookup table so I can input a HEX errorcode and return the actual message as a string. For example
Hashtable EDSDKErrorCodes = new Hashtable();
EDSDKErrorCodes.Add("0x00008D01", "EDS_ERR_TAKE_PICTURE_AF_NG");
EDSDKErrorCodes.Add("0x00008D08", "EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG");
etc etc...
The later in my code I could grab the error message returned from one of the EDSDK methods and display it in a human readable form :
errorcode= EDSDK.EdsInitializeSDK();
MessageBox.Show(Convert.ToString(EDSDKErrorCodes[errorcode]));
My question is .. is this a good way to handle all these error codes or is there a better and more efficient way that people would recommend? If so how I could fill my hash table as a series of constants rather than having to use the Add method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一种方法是创建一个带有描述属性的枚举。有关完整详细信息,请参阅此处。这是总结它的外观:
最简单的是,如果您不关心描述,您可以使用 Enum.GetName
Another way you could do this is by creating an Enum with a description attribute. See here for the full details. This is summarizing how it would look:
At the very simplest, if you didnt care about a description, you could convert the enum name to string using Enum.GetName
您可以为此使用枚举。
那么您的用法可能如下所示:
You could use an enum for this.
Then your usage could be something like the following:
我认为制作一个枚举会更好:
您只需将十六进制转换为整数即可。然后你可以简单地这样调用它:
如果你想要人工格式化的错误描述,那么我会建议某种映射,例如:
I would think making an enum would be superior:
You simply do the conversion from hex to integer. Then you can simply call it like so:
If you want human formatted error descriptions, then I would suggest some kind of mapping like:
您可以使用通用字典而不是哈希表,
然后您可以通过执行操作来检索您想要的值
对于添加,我认为您可能可以通过反射执行一些操作来反映 EDSDK 类并找到所有常量 uint 成员。然后遍历该列表添加值和常量名称。
You could use a Generic Dictionary instead of a hashtable
You can then retrieve the value you want by doing
For adding, I think you might be able to do something with reflection to reflect over the EDSDK class and find all the constant uint members. Then iterate through that list adding the value and the constant name.
好吧,自从我们开始工作以来,又采取了另一种方式:-)
如果当前错误代码未定义,此解决方案还会生成人类可读的错误字符串。
像这样创建一个
Enum
:然后,创建一个 扩展方法 对于像这样的枚举类型:
最后,像这样使用它:
Ok, another take since we are rolling :-)
This solution also produces a human-readable error string if the current error code is not defined.
Create an
Enum
like so:Then, create an Extension Method for the enum type like this:
And finally, use it like that:
首先,您应该使用通用字典而不是哈希表。其次,我对 Canon EDSDK 一无所知,但奇怪的是它返回一个十六进制字符串的错误代码,而不是定义常量时简单的 uint 。他们真的就是这样被退回的吗?如果是这样,那就很奇怪,但是您应该能够将它们转换为整数,否则,您可以跳过该步骤。
如果您想使用反射创建数组,您可以执行以下操作:
然后您可以使用以下方式访问它:
如果 errorCode 是字符串,请首先使用以下方式转换它:
First of all, you should use a generic dictionary instead of a hashtable. Second, I don't know anything about the Canon EDSDK, but it seems odd that it returns an error code as a hex string rather than simply a uint as the constants are defined. Is that really how they are being returned? If so, that's weird, but you should be able to convert them to an integer, otherwise, you can skip that step.
If you want to create the array using reflection, you could do something like this:
And you can then access it using:
If errorCode is a string, convert it first using: