Skip to content

Commit

Permalink
Merge pull request #29 from AndreiCsibi-msft/NotDefaultOnStructs
Browse files Browse the repository at this point in the history
Adding NotDefault method to check structs values against default values.
  • Loading branch information
AArnott authored Mar 26, 2019
2 parents 1dced06 + 315ff60 commit 9123241
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/Validation/Requires.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static T NotNullAllowStructs<T>([ValidatedNotNull]T value, string paramet
public static void NotNullOrEmpty([ValidatedNotNull]string value, string parameterName)
{
// To the guy that is doing random code cleaning:
// Consider the perfomance when changing the code to delegate to NotNull.
// Consider the performance when changing the code to delegate to NotNull.
// In general do not chain call to another function, check first and return as earlier as possible.
if (value == null)
{
Expand All @@ -150,7 +150,7 @@ public static void NotNullOrEmpty([ValidatedNotNull]string value, string paramet
public static void NotNullOrWhiteSpace([ValidatedNotNull]string value, string parameterName)
{
// To the guy that is doing random code cleaning:
// Consider the perfomance when changing the code to delegate to NotNull.
// Consider the performance when changing the code to delegate to NotNull.
// In general do not chain call to another function, check first and return as earlier as possible.
if (value == null)
{
Expand Down Expand Up @@ -180,7 +180,7 @@ public static void NotNullOrWhiteSpace([ValidatedNotNull]string value, string pa
public static void NotNullOrEmpty([ValidatedNotNull]System.Collections.IEnumerable values, string parameterName)
{
// To the guy that is doing random code cleaning:
// Consider the perfomance when changing the code to delegate to NotNull.
// Consider the performance when changing the code to delegate to NotNull.
// In general do not chain call to another function, check first and return as earlier as possible.
if (values == null)
{
Expand Down Expand Up @@ -316,6 +316,25 @@ public static void Defined<T>(T value, string parameterName)
}
}

/// <summary>
/// Throws an <see cref="ArgumentException"/> if the specified parameter's value is equal to the
/// default value of the <see cref="Type"/> <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <c>null</c> or empty.</exception>
[DebuggerStepThrough]
public static void NotDefault<T>(T value, string parameterName)
where T : struct
{
var defaultValue = default(T);
if (defaultValue.Equals(value))
{
throw new ArgumentException(Format(Strings.Argument_StructIsDefault, parameterName, typeof(T).FullName), parameterName);
}
}

/// <summary>
/// Throws an ArgumentException if a condition does not evaluate to true.
/// </summary>
Expand Down
11 changes: 10 additions & 1 deletion src/Validation/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Validation/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
<data name="Argument_NullElement" xml:space="preserve">
<value>'{0}' cannot contain a null (Nothing in Visual Basic) element.</value>
</data>
<data name="Argument_StructIsDefault" xml:space="preserve">
<value>'{0}' cannot be the default value defined by '{1}'.</value>
</data>
<data name="Argument_Whitespace" xml:space="preserve">
<value>The parameter "{0}" cannot consist entirely of white space characters.</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions src/Validation/Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<AssemblyOriginatorKeyFile>opensource.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Update="Strings.resx">
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<None Include="tools\**">
<Pack>true</Pack>
Expand Down

0 comments on commit 9123241

Please sign in to comment.