gpt4 book ai didi

c# - 如何从具有通用返回类型的方法中抛出异常

转载 作者:行者123 更新时间:2023-11-30 23:23:12 25 4
gpt4 key购买 nike

在构建用于获取属性值的扩展方法时,我具有以下方法签名:

public static IEnumerable<TValue> GetAttributeValues<TAttribute, TValue>(
this Type type,
string methodName,
Type[] parameterTypes,
Func<TAttribute, TValue> valueSelector,
bool inherit = false) where TAttribute : Attribute

但是,在测试过程中,我发现我无法有效地在此方法中抛出异常。如果我抛出 ArgumentException 的实例,执行过程似乎会忽略它,,异常不会冒泡。调用方法中的 try...catch 什么也没捕获。

如果我对方法调用的结果调用 GetType(),类型将显示为方法的完全限定名称。

我在调试时也无法进入该方法。

谁能解释为什么我无法用异常停止应用程序,即使这个方法所做的只是抛出异常?

最佳答案

因为您的扩展方法返回一个 IEnumerable<T>你需要调用 ToArrayFirst否则它不会枚举。

关于c# - 如何从具有通用返回类型的方法中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com