Best exception for an invalid generic type argument

后端 未结 11 1194
孤城傲影
孤城傲影 2020-12-08 18:21

I\'m currently writing some code for UnconstrainedMelody which has generic methods to do with enums.

Now, I have a static class with a bunch of methods which are

11条回答
  •  抹茶落季
    2020-12-08 18:33

    Apparently, Microsoft uses ArgumentException for that, as demonstrated on example of Expression.Lambda<>, Enum.TryParse<> or Marshal.GetDelegateForFunctionPointer<> in Exceptions section. I couldn't find any example indicating otherwise, either (despite searching local reference source for TDelegate and TEnum).

    So, I think it's safe to assume that at least in Microsoft code it's a common practice to use ArgumentException for invalid generic type arguments aside from basic variable ones. Given that the exception description in docs doesn't discriminate between those, it's not too much of a stretch, either.

    Hopefully it decides the question things once and for all.

提交回复
热议问题