A pity about generics

Source: Internet
Author: User
Tags throw exception
In the project, I defined a throwhelper helper class to throw custom exceptions in the project. For example, an exception is defined as follows:
Public class employeeexception: applicationexception
{
Public employeeexception ()
: Base ()
{}

Public employeeexception (string message)
: Base (Message)
{}

Public employeeexception (string message, exception innerexception)
: Base (message, innerexception)
{}< BR >}< br> we can define a method to throw this exception in throwhelper for the caller to call:
Public static class throwhelper
{< br> Public static employeeexception throwemployeeexception (string message)
{< br> logservice. error (Message);
throw new employeeexception (Message);
}

Public static employeeexception throwemployeeexception (string message, exception innerexception)
{
Logservice. Error (message, innerexception );
Throw new employeeexception (message, innerexception );
}
}
One problem is that when we define multiple custom exceptions, we need to constantly add new methods to throwhelper. This is like recording a flow log. Developers cannot like such repetitive work.

So can we use generics to solve this problem? For example, define the generic version of the helper class:
Public static class throwhelpergeneric <tcustomexception>
Where tcustomexception: applicationexception, new ()
{
Public static tcustomexception throwcustomexception (string message)
{
Logservice. Error (Message );
Throw new tcustomexception (Message );
}

Public static tcustomexception throwcustomexception (string message, exception innerexception)
{
Logservice. Error (message, innerexception );
Throw new tcustomexception (message, innerexception );
}
}
Unfortunately, generic types do not support constructors with parameters. To solve this problem, only reflection technology is used. The implementation is as follows:
Public static class throwhelpergeneric <tcustomexception>
Where tcustomexception: applicationexception, new ()
{
Public static tcustomexception throwcustomexception (string message)
{
Logservice. Error (Message );
Tcustomexception exception = (tcustomexception) typeof (tcustomexception). getconstructor (New Type [] {typeof (string )}).
Invoke (new object [] {message });

Throw exception;
}

Public static tcustomexception throwcustomexception (string message, exception innerexception)
{< br> logservice. error (message, innerexception);
tcustomexception exception = (tcustomexception) typeof (tcustomexception ). getconstructor (New Type [] {typeof (string), typeof (exception )}).
invoke (new object [] {message, innerexception});

throw exception;
}< BR >}< br> however, I am sorry that since new () can be used in the where constraint of the generic type, why cannot we provide constructor constraints with parameters? For example,
Public static class throwhelpergeneric
where tcustomexception: applicationexception, new (), new (string), new (string, exception)
{< br> Public static tcustomexception throwcustomexception (string message)
{< br> logservice. error (Message);
throw new tcustomexception (Message);
}

Public static tcustomexception throwcustomexception (string message, exception innerexception)
{< br> logservice. error (message, innerexception);
throw new tcustomexception (message, innerexception );
}< BR >}< br> I wonder if such generic support will be available in future C # versions? Is it quite difficult to implement such a syntax?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.