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?