【发布时间】:2013-08-06 08:13:07
【问题描述】:
我有一个“工厂”类,它应该比较通用类类型参数并返回对象的特定实例:
public static class MyExceptionFactory<T> where T: System.Exception {
public static MyReturnObj Create() {
// return instance of MyReturnObj based on type of T
}
}
但我无法检查例如T 是 ArgumentNullException 因为 T 是类型参数而不是变量
if(T is ArgumentNullException) // won't work
.. 而且,我无法检查 T 的类型
if(typeof(T) is ArgumentNullException)
因为 IntelliSense 告诉我 T 永远不是 System.ArgumentNullException(我假设因为 T 是 System.Exception)
我该如何解决这个问题?我是否必须传递一个 System.Exception 的实例来检查它的类型,还是有任何其他方法可以通过类类型参数来做到这一点?
【问题讨论】:
标签: c# .net exception generics types