Consider the following issue
When designing a framework, an interface exposing some event is presented
interface I
{
event MyEventHandler MyEvent
}
This interface will eventually be implemented by many different 3rd party vendors, and may be consumed by various clients.
Since each vendor may new up the event args with invalid data, the only control I have as a framework author is at the event args level, so I thought of the following pattern:
class MyEventArgs
{
public int? MyProperty{get;}
MyEventArgs(int arg)
{
if(arg.IsInvalidArgument())//Let's pretend that there's such an extension method
throw new ArgumentException(...)
MyProperty = arg;
}
This ensures that a client can not use invalid values provided by some rogue piece of code, since the constructor throws an exception, hence the integer will have no value assigned, making it a null reference.
However, this also creates overhead in client code, since now the client has to check the HasValue and then access Value, making the EventArgument less user friendly.. This becomes even more cumbersome when the amount of parameters per event argument grow.
I could technically remove the question mark, which would rid the client of the Nullable nonsense, since in my opinion there is no way on god's green earth to obtain a reference to such an instance, but the problem is that this scenario, although easy to test, may have edge cases which I never thought of, hence my question.
Is there any possible way to obtain a reference to an instance whose constructor had thrown an exception and pass it to the event listeners?