I have the following extension method:
public static void ThrowIfArgumentIsNull<T>(this T value, string argument) where T : class { if (value == null) { throw new ArgumentNullException(argument); } }
and this is an example of its usage....
// Note: I've poorly named the argument, on purpose, for this question. public void Save(Category qwerty) { qwerty.ThrowIfArgumentIsNull("qwerty"); .... }
works 100% fine.
But, I don't like how I have to provide the name of the variable, just to help my exception message.
I was wondering if it's possible to refactor the extension method, so it could be called like this...
qwerty.ThrowIfArgumentIsNull();
and it automatically figures out that the name of the variable is 'qwerty' and therefore uses that as the value for the ArgumentNullException.
Possible? I'm assuming reflection could do this?
You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called.
In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type.
An extension method must be a static method. An extension method must be inside a static class -- the class can have any name. The parameter in an extension method should always have the "this" keyword preceding the type on which the method needs to be called.
What is extension method? Extension methods in C# are methods applied to some existing class and they look like regular instance methods. This way we can "extend" existing classes we cannot change. Perhaps the best example of extension methods are HtmlHelper extensions used in ASP.NET MVC.
No, you can't do this. It would be nice, but it's not possible without some sort of AOP getting involved. I'm sure PostSharp can do a nice job, hopefully using attributes, and in Code Contracts it would just be:
Contract.Requires(qwerty != null);
Ideally I'd like a PostSharp attribute which generates the Code Contracts call - and I'll play around with that at some point - but until then, the extension method you've got is the best approach I've found...
(If I ever try the PostSharp + Code Contracts approach, I'll certainly blog about it, btw... Mono Cecil might make it reasonably easy too.)
EDIT: To expand on Laurent's answer, you could potentially have:
new { qwerty }.CheckNotNull();
And if you had lots of non-nullable parameters, you could have:
new { qwerty, uiop, asdfg }.CheckNotNull();
This would have to use reflection to work out the properties. There are ways that you could avoid doing the reflection on every access, building a delegate for each property and generally making it whizzy. I may investigate this for a blog post... but it's somewhat icky, and I prefer the idea of being able to just attribute the parameters...
EDIT: Code implemented, and blog post duly made. Ick, but fun.
In a word: no.
The extension method is passed a value. It has no idea where the value comes from or what identifier the caller may have choosen to refer to it as.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With