要求通用方法在FAIL上抛出特定的异常类型 [英] Asking a Generic Method to Throw Specific Exception Type on FAIL

查看:96
本文介绍了要求通用方法在FAIL上抛出特定的异常类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道我完全是这样看待一个傻子,但我的大脑今天早上只是没有踢进齿轮。



我想有一个方法可以说如果它坏了,返回这种类型的异常,对吗?



例如, (,但不起作用):

  static ExType TestException< ExType>(string message )其中ExType:异常
{
异常ex1 =新异常();
ExType ex = new Exception(message);
return ex;

$ / code>

现在让我困惑的是我们知道泛型类型将由于 where 子句而成为Exception类型。但是,代码失败是因为我们无法隐式地将 Exception 转换为 ExType 。我们不能明确地转换它,例如:

  static ExType TestException< ExType>(string message)其中ExType:Exception 
{
异常ex1 = new Exception();
ExType ex =(ExType)(new Exception(message));
return ex;
}

因为那也失败了。所以是这样的事情?我有一种强烈的感觉,它会变得非常简单,但是我对这个老头非常辛苦,所以给我一些松懈:P






更新



感谢各位回复,看起来不是我完成 >白痴! ;)



好的,所以 Vegard Sam 让我可以实例化正确的类型,但显然由于消息参数是只读的实例化。

马特用他的回应击中了他的头部,我测试了这一切,并且所有的工作都很好。下面是示例代码:

  static ExType TestException< ExType>(string message)其中ExType:Exception,new()
{
ExType ex =(ExType)Activator.CreateInstance(typeof(ExType),message);
return ex;
}

甜! :)

谢谢你们!

解决方案

几乎可以做到这一点如下所示:

  static void TestException< E>(string message)其中E:Exception,new()
{
var e = new E();
e.Message = message;
throw e;
}

然而,这并不会编译,因为Exception.Message是只读的。它只能通过将它传递给构造函数来分配,并且没有办法用一个默认的构造函数来限制一个泛型类型。



我想你会有使用反射(Activator.CreateInstance)来使用消息参数新增自定义异常类型,如下所示:

  static void TestException< E>(字符串消息)E:异常
{
将Activator.CreateInstance(typeof(E),message)作为E;
}

编辑哎呀只是意识到你想要返回异常,而不是抛出异常。同样的原则适用,所以我会留下我的答案与throw语句一样。


Right, I know I am totally going to look an idiot with this one, but my brain is just not kicking in to gear this morning.

I want to have a method where I can say "if it goes bad, come back with this type of Exception", right?

For example, something like (and this doesn't work):

    static ExType TestException<ExType>(string message) where ExType:Exception
    {
        Exception ex1 = new Exception();
        ExType ex = new Exception(message);
        return ex;
    }

Now whats confusing me is that we KNOW that the generic type is going to be of an Exception type due to the where clause. However, the code fails because we cannot implicitly cast Exception to ExType. We cannot explicitly convert it either, such as:

    static ExType TestException<ExType>(string message) where ExType:Exception
    {
        Exception ex1 = new Exception();
        ExType ex = (ExType)(new Exception(message));
        return ex;
    }

As that fails too.. So is this kind of thing possible? I have a strong feeling its going to be real simple, but I am having a tough day with the old noggin, so cut me some slack :P


Update

Thanks for the responses guys, looks like it wasn't me being a complete idiot! ;)

OK, so Vegard and Sam got me on to the point where I could instantiate the correct type, but then obviously got stuck because the message param is read-only following instantiation.

Matt hit the nail right on the head with his response, I have tested this and all works fine. Here is the example code:

    static ExType TestException<ExType>(string message) where ExType:Exception, new ()
    {
        ExType ex = (ExType)Activator.CreateInstance(typeof(ExType), message);
        return ex;
    }

Sweet! :)

Thanks guys!

解决方案

You can almost do it like this:

static void TestException<E>(string message) where E : Exception, new()
{
    var e = new E();
    e.Message = message;
    throw e;
}

However, that doesn't compile because Exception.Message is read only. It can only be assigned by passing it to the constructor, and there's no way to constrain a generic type with something other than a default constructor.

I think you'd have to use reflection (Activator.CreateInstance) to "new up" the custom exception type with the message parameter, like this:

static void TestException<E>(string message) where E : Exception
{
    throw Activator.CreateInstance(typeof(E), message) as E;
}

Edit Oops just realised you're wanting to return the exception, not throw it. The same principle applies, so I'll leave my answer as-is with the throw statements.

这篇关于要求通用方法在FAIL上抛出特定的异常类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆