为什么 InvalidEnumArgumentException 在 Silverlight 4 中已过时?

发布于 2024-12-06 17:29:19 字数 451 浏览 0 评论 0 原文

我惊讶地发现 < code>InvalidEnumArgumentException 已在 Silverlight 4 中过时。

有谁知道这是为什么?我发现这是一个非常有用的异常,特别是在手动将二进制数据反序列化为枚举值时。

[ObsoleteAttribute(
  "InvalidEnumArgumentException is obsolete. Use ArgumentException instead.")]
public class InvalidEnumArgumentException : Exception

I was surprised to discover that InvalidEnumArgumentException has been made obsolete in Silverlight 4.

Does anyone know why this is? I found this to be quite a useful exception, especially when manually deserialising binary data to enum values.

[ObsoleteAttribute(
  "InvalidEnumArgumentException is obsolete. Use ArgumentException instead.")]
public class InvalidEnumArgumentException : Exception

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

灯下孤影 2024-12-13 17:29:19

你是对的,它被标记为已过时,但完全存在(这里是 指向该类的 Silverlight 版本的正确链接

我认为这个特定的“为什么”问题对于这里的每个人来说都很难回答,因为他们不在微软工作并且参与审查此类变更的过程。微软可能有某种文档解释了在当前版本中将其标记为过时的高级原因。现在就是这样,我担心你可能不得不忍受它。

出于兴趣,我用 bing 搜索了一下,这里的这个帖子是我能找到的关于该主题的最佳帖子。即使在 Silverlight.net 上查找也不会产生任何结果。因此,您要么需要一份金牌合作伙伴合同(或其他任何名称),然后直接联系 Microsoft 的人员来解决此问题。

然而,对于这个同样的异常似乎存在一些争议,无论是否使用它是好的做法,我想引用对此的评论 链接,谈论不一致:

不幸的是,由于 InvalidEnumArgumentException 是在
System.dll 而不是 mscorlib.dll,后者在
无效的枚举参数被传递给成员,而是抛出
ArgumentException 或 ArgumentOutOfRangeException。这种不一致
然而,通常不会出现问题,因为这个例外,当
抛出,通常表明调用者中存在错误,并且很少被捕获
在 catch 子句中。

因此,这可能也是导致将其从 Silverlight 中删除的整个情况的原因。或不。也许只是因为他们认为这是不必要的开销,可以这么说,因为你可能无论如何都会捕获 ArgumentException,并且大多数实现通过进一步分解它不会获得任何好处。这只是一个猜测,但我担心你不会得到比这更好的结果(除了其他随机猜测)。

如果您愿意,您当然可以添加自己的 InvalidEnumArgumentException 实现,而且我猜您已经这样做了。

You are right, it is marked obsolete but totally there (here is the correct link pointing to the Silverlight Version of the class)

I think this particular "Why" question is hard to answer for everyone here, who is not working at microsoft and involved in the process of reviewing such changes. There is probably some kind of document at Microsoft explaining the high-level reasons for marking it obsolete in the current version. It is as it is right now and i fear you might have to live with it.

Out of intereset i googled a bit with bing and this SO thread here was the best hit on the topic i could find. Even looking for it on Silverlight.net die not yield any results. So either you need a gold partner contract (or whatever its called) and contact the guys at Microsoft directly on the issue.

However there seems a bit controvery going on over this very same Exception, wether its good practice to use it or not, i would like to quote a comment on this link, talking about inconsistensies:

Unfortunately, as InvalidEnumArgumentException is defined in
System.dll and not mscorlib.dll, the later does not throw it when an
invalid enum argument is passed to a member, but instead throws
ArgumentException or ArgumentOutOfRangeException. This inconsistancy
however, usually does not present a problem, as this exception, when
thrown, typically indicates a bug in the caller and is rarely caught
within a catch clause.

So that might have also played into the whole circumstance that lead to removing it from Silverlight. Or not. Maybe its just because they thought its unneeded overhead, so to say, because you are probably catching ArgumentExceptions anyway and most implementatoins gain nothing by further breaking it down. Its just a guess, but i am afraid you wont get any better than this (besides other random guesses).

You could of course add your own InvalidEnumArgumentException implementation if you wanted to, and i would guess that you have already done so.

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