有关在 C# 中构建错误代码查找的建议

发布于 2024-11-07 19:35:27 字数 817 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

剩余の解释 2024-11-14 19:35:27

另一种方法是创建一个带有描述属性的枚举。有关完整详细信息,请参阅此处。这是总结它的外观:

public enum ErrorCodes : uint
{
    [Description("Description for EDS_ERR_TAKE_PICTURE_CARD_NG")]
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    [Description("Description for EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG")]
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

最简单的是,如果您不关心描述,您可以使用 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:

public enum ErrorCodes : uint
{
    [Description("Description for EDS_ERR_TAKE_PICTURE_CARD_NG")]
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    [Description("Description for EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG")]
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

At the very simplest, if you didnt care about a description, you could convert the enum name to string using Enum.GetName

兔姬 2024-11-14 19:35:27

您可以为此使用枚举。

public enum ErrorCodes : uint
{
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

那么您的用法可能如下所示:

ErrorCodes error = (ErrorCodes)EDSDK.EdsInitializeSDK();
MessageBox.Show(Enum.GetName(typeof(ErrorCodes), error));

You could use an enum for this.

public enum ErrorCodes : uint
{
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

Then your usage could be something like the following:

ErrorCodes error = (ErrorCodes)EDSDK.EdsInitializeSDK();
MessageBox.Show(Enum.GetName(typeof(ErrorCodes), error));
未央 2024-11-14 19:35:27

我认为制作一个枚举会更好:

public enum CanonErrorCode
{
    SomeErrorDescription = 14,
    SomeOtherErrorDescription = 15
    // .. etc
}

您只需将十六进制转换为整数即可。然后你可以简单地这样调用它:

// Samples
var myErrorCode = (CanonErrorCode)someIntegerValue;
var myErrorCode = CanonErrorCode.SomeOtherErrorDescription;

如果你想要人工格式化的错误描述,那么我会建议某种映射,例如:

static Dictionary<int, string> errorlookups = new Dictionary<int, string>();

errorLookups.Add(10, "This is the human readable error message.");
errorLookups.Add(17, "Create this guy once in a static constructor.");

I would think making an enum would be superior:

public enum CanonErrorCode
{
    SomeErrorDescription = 14,
    SomeOtherErrorDescription = 15
    // .. etc
}

You simply do the conversion from hex to integer. Then you can simply call it like so:

// Samples
var myErrorCode = (CanonErrorCode)someIntegerValue;
var myErrorCode = CanonErrorCode.SomeOtherErrorDescription;

If you want human formatted error descriptions, then I would suggest some kind of mapping like:

static Dictionary<int, string> errorlookups = new Dictionary<int, string>();

errorLookups.Add(10, "This is the human readable error message.");
errorLookups.Add(17, "Create this guy once in a static constructor.");
吐个泡泡 2024-11-14 19:35:27

您可以使用通用字典而不是哈希表,

Dictionary<uint, String> myDict = new Dictionary<uint, String>();

然后您可以通过执行操作来检索您想要的值

MessageBox.Show(myDict[errorCode]);

对于添加,我认为您可能可以通过反射执行一些操作来反映 EDSDK 类并找到所有常量 uint 成员。然后遍历该列表添加值和常量名称。

You could use a Generic Dictionary instead of a hashtable

Dictionary<uint, String> myDict = new Dictionary<uint, String>();

You can then retrieve the value you want by doing

MessageBox.Show(myDict[errorCode]);

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.

我还不会笑 2024-11-14 19:35:27

好吧,自从我们开始工作以来,又采取了另一种方式:-)
如果当前错误代码未定义,此解决方案还会生成人类可读的错误字符串。

像这样创建一个 Enum

public enum ErrorCode : uint
{
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

然后,创建一个 扩展方法 对于像这样的枚举类型:

public static class ErrorHandler
{
    public static string ToErrorString(this ErrorCode errorCode)
    {
        return Enum.IsDefined(typeof(ErrorCode), errorCode) ? 
            errorCode.ToString() : "Undefined error code";
    }
}

最后,像这样使用它:

var errorCode = (ErrorCode)EDSDK.EdsInitializeSDK();
MessageBox.Show(errorCode.ToErrorString());

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:

public enum ErrorCode : uint
{
    EDS_ERR_TAKE_PICTURE_CARD_NG = 0x00008D07,
    EDS_ERR_TAKE_PICTURE_CARD_PROTECT_NG = 0x00008D08
}

Then, create an Extension Method for the enum type like this:

public static class ErrorHandler
{
    public static string ToErrorString(this ErrorCode errorCode)
    {
        return Enum.IsDefined(typeof(ErrorCode), errorCode) ? 
            errorCode.ToString() : "Undefined error code";
    }
}

And finally, use it like that:

var errorCode = (ErrorCode)EDSDK.EdsInitializeSDK();
MessageBox.Show(errorCode.ToErrorString());
我不是你的备胎 2024-11-14 19:35:27

首先,您应该使用通用字典而不是哈希表。其次,我对 Canon EDSDK 一无所知,但奇怪的是它返回一个十六进制字符串的错误代码,而不是定义常量时简单的 uint 。他们真的就是这样被退回的吗?如果是这样,那就很奇怪,但是您应该能够将它们转换为整数,否则,您可以跳过该步骤。

如果您想使用反射创建数组,您可以执行以下操作:

Dictionary<int,String> EDSDKErrorCodes = new Dictionary<int,String>;
System.Reflection.FieldInfo[] fields = typeof(EDSDK).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
foreach (System.Reflection.FieldInfo field in fields) {
  EDSDKErrorCodes[(uint)field.GetValue(null)] = field.Name;
}

然后您可以使用以下方式访问它:

MessageBox.Show(EDSDKErrorCodes[errorCode]);

如果 errorCode 是字符串,请首先使用以下方式转换它:

uint errorNumber = uint.Parse(errorCode.Substring(2), System.Globalization.NumberStyle.HexNumber);

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:

Dictionary<int,String> EDSDKErrorCodes = new Dictionary<int,String>;
System.Reflection.FieldInfo[] fields = typeof(EDSDK).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
foreach (System.Reflection.FieldInfo field in fields) {
  EDSDKErrorCodes[(uint)field.GetValue(null)] = field.Name;
}

And you can then access it using:

MessageBox.Show(EDSDKErrorCodes[errorCode]);

If errorCode is a string, convert it first using:

uint errorNumber = uint.Parse(errorCode.Substring(2), System.Globalization.NumberStyle.HexNumber);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文