c# - Assign Generic Exception a message -



c# - Assign Generic Exception <TException> a message -

since lot of argument null checking, wanted simplify little bit. created next method:

public static void throwexceptionif<texception>(bool condition, string message = null, params keyvaluepair<string, string>[] data) texception : exception, new() { if (condition) { return; } var exception = new texception(); data.asparallel().forall(d => exception.data.add(d.key, d.value)); throw exception; }

which used like:

public validatablebase(iuser user, ieventaggregator eventservice) : this() { exceptionfactory .throwexceptionif<argumentnullexception>(user == null || eventservice == null); this.user = user; this.eventservice = eventservice; }

the problem can't assign message exception. message property readonly , generic doesn't see constrained type accepts parameters.

i've seen people instance customexception , pass message in through constructor, , assign innerexception given exception (in case texception) wanted inquire if there alternatives before going route. wrapping exceptions thrown mill in custom exception seems bad design.

edit

the final mill uses combination of everyones answers , works great.

public static void throwexceptionif<texception>(bool condition, string message = null, iuser user = null, params keyvaluepair<string, string>[] data) texception : exception, new() { throwexceptionif<texception>( condition, () => (texception) activator.createinstance(typeof (texception), message), user, data); } public static void throwexceptionif<texception>(func<bool> predicate, string message = null, iuser user = null, params keyvaluepair<string, string>[] data) texception : exception, new() { throwexceptionif<texception>(predicate(), message, user, data); } public static void throwexceptionif<texception>(bool condition, func<texception> exception, iuser user = null, params keyvaluepair<string, string>[] data) texception : exception, new() { if (condition) { return; } texception exceptiontothrow = exception(); addexceptiondata(exceptiontothrow, user, data); throw exceptiontothrow; } public static void addexceptiondata(exception exception, iuser user = null, params keyvaluepair<string, string>[] data) { foreach (var exceptiondata in data) { exception.data.add(exceptiondata.key, exceptiondata.value); } if (user != null) { exception.data.add("user", user.racfid); } }

i can utilize throwexceptionif method in next ways

throwexceptionif<argumentnullexception>( user == null, "users can not null."); throwexceptionif<argumentnullexception>( user == null, () => new argumentnullexception("user", "users can not null")); throwexceptionif<argumentexception>( () => user.firstname.equals(user.lastname), "last name must not equal first name");

thanks help!

you can pass creation lambda method, rather message, throwing method:

public static void throwexceptionif<texception>(bool condition, func<texception> init, params keyvaluepair<string, string>[] data) texception : exception { if (condition) { return; } var exception = init(); data.asparallel().forall(d => exception.data.add(d.key, d.value)); throw exception; }

then phone call throwexceptionif(true, ()=>new argumentnullexception("foo"));.

c# exception generics factory-pattern

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -