-
Notifications
You must be signed in to change notification settings - Fork 55
Add support for 'CallerArgumentExpression' attribute #58
Comments
The attribute has been added to the BCL but it does not actually do anything yet: dotnet/csharplang#287 |
Oh wow! I was one of those who got misled by the documentation. Had no idea this was not yet supported. Thanks @alexrp for clarifying. I wonder though, would it make sense to add the needed code to support it right now, so that once it starts working there is no extra work to do? Since they have already published the attribute it's unlikely it will suffer any further breaking changes. |
Unlikely may not mean certain. Still, certain as of a few days ago this attribute will be available starting from C# 10 onwards. See dotnet/roslyn/pull/54839 |
In the meantime - in order to utilize this functionality - you can make a small abstraction on top of this library in your own code: public static class MyGuard
{
public static Dawn.Guard.ArgumentInfo<T> Argument<T>(T argument, [CallerArgumentExpression("argument")] string? argumentName = default)
{
return Dawn.Guard.Argument(argument, argumentName);
}
} public class GuardTests
{
[Fact]
public void SourcesMemberName()
{
// Arrange
var expectedErrorMessage = $"year cannot be less than 0. (Parameter 'year'){Environment.NewLine}Actual value was -1.";
// Act
var ex = Assert.Throws<ArgumentOutOfRangeException>(() => new Example(-1));
// Assert
Assert.Equal(expectedErrorMessage, ex.Message);
}
private class Example
{
public Example(int year)
{
MyGuard.Argument(year).Min(0);
}
}
} |
@safakgur now that C#10 has officially launched, do you know when support for |
Sorry everyone, I neglected Guard a lot during the last year because of other responsibilities (the pandemic definitely didn't help). But I'm still working on v2, our next major version which I hope to release before March. @julealgon - Yes, it's coming with v2! The new version will also also have some generic validations that accept a bool condition marked with this attribute, so both the parameter name and the message containing the failed condition can be retrieved automatically (Think of With that said, v2 will include a lot of other changes, some of which will be breaking changes, so you may not want to migrate a sizeable solution to use Guard before that. I hope that helps! |
@safakgur do you have any updates to share on v2? |
Hi folks, this was one of the things I wanted for v2 but I'm retiring Guard - I apologise for any inconvenience, and thank you for all the support. The readme has details. |
Is your feature request related to a problem?
The
(value, nameof(value))
way of specifying the argument name is not necessary anymore when using C#8, since theCallerArgumentExpressionAttribute
has been added to capture the name of any argument passed into the method.Describe the solution you'd like
The
Argument
method should rely on[CallerArgumentExpression]
to fetch the argument name automatically without having to pass it from the caller. This would considerably clean up the usage of the library while also discouraging the use of theExpression
-based approach which is very slow.I'm actually quite surprised that this hasn't been implemented yet after reading all the documentation. If authors are aware of such attribute, I'd like to read the reasons why this hasn't been put in place yet (besides being unavailable in older runtimes).
The text was updated successfully, but these errors were encountered: