Comments (9)
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
from guard.
In the meantime
@safakgur now that C#10 has officially launched, do you know when support for CallerArgumentExpression
will be in place? I'm actually planning a migration to Guard in a sizeable solution soon and it would be really nice to have this supported by then as it would remove the need for an additional cleanup pass in all the code removing the parameter name argument later.
from guard.
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 Argument(age).Range(age >= 18)
throwing an ArgumentOutOfRangeException
with the param name "age" and the message "Precondition failed: age >= 18").
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!
from guard.
The attribute has been added to the BCL but it does not actually do anything yet: dotnet/csharplang#287
from guard.
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.
from guard.
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);
}
}
}
from guard.
I'm still working on v2, our next major version which I hope to release before March.
@safakgur do you have any updates to share on v2?
from guard.
from guard.
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.
from guard.
Related Issues (20)
- Interop NotNullWhenAttribute leaks HOT 1
- Support for both directory and file path validation HOT 2
- How to return the Exception instead of throwing it HOT 3
- Problem with "InitContainsDuplicate" on .NET < 4.7.2 HOT 1
- Multiple checks with same custom message HOT 3
- How to Guard an ICollection member HOT 1
- CA1062 warning HOT 5
- Support validation/guard-clauses for collection items HOT 1
- Guard variants for nullable types HOT 2
- how to validata the string is a valid email address? HOT 1
- NotDefault limited to struct HOT 1
- Unacceptable performance HOT 3
- Confirm parameter is one of list of Types HOT 1
- Binary Backward Compatibility HOT 2
- Document inclusivity of InRange HOT 2
- Similar fluent-style guard library for javascript
- Enhanced String char based guards. HOT 2
- 'NotNull' validation should be available for unconstrained generic types HOT 2
- Status on v2 of the library HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from guard.