Skip to content

Commit 5a609df

Browse files
committed
Update
1 parent 58a918f commit 5a609df

3 files changed

Lines changed: 96 additions & 13 deletions

File tree

src/Tests/StructTest/Program.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@ internal class Program
77

88
private static void Main(string[] args)
99
{
10-
var m1 = new Measurement();
11-
Console.WriteLine(m1); // output: NaN (Undefined)
10+
//var m1 = new Measurement();
11+
//Console.WriteLine(m1); // output: NaN (Undefined)
1212

13-
var m2 = default(Measurement);
14-
Console.WriteLine(m2); // output: 0 ()
13+
//var m2 = default(Measurement);
14+
//Console.WriteLine(m2); // output: 0 ()
1515

16-
var ms = new Measurement[2];
17-
Console.WriteLine(string.Join(", ", ms)); // output: 0 (), 0 ()
16+
//var ms = new Measurement[2];
17+
//Console.WriteLine(string.Join(", ", ms)); // output: 0 (), 0 ()
1818

19-
ValueTypeTest.ShowSize();
19+
//ValueTypeTest.ShowSize();
2020

21-
ValueTypeTest.OutOfPrecisionTrueTest();
22-
ValueTypeTest.OutOfPrecisionFalseTest();
21+
//ValueTypeTest.OutOfPrecisionTrueTest();
22+
//ValueTypeTest.OutOfPrecisionFalseTest();
2323

24-
Console.WriteLine();
25-
ValueTypeTest.EffectiveLength_4();
24+
//Console.WriteLine();
25+
//ValueTypeTest.EffectiveLength_4();
26+
27+
//Console.WriteLine();
28+
//ValueTypeTest.RoundAlgorithm();
2629

30+
ValidationTest.ValidationError();
2731
Console.WriteLine();
28-
ValueTypeTest.RoundAlgorithm();
32+
ValidationTest.ValidationAllow();
2933
}
3034

3135
#endregion
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace StructTest;
4+
5+
public static class ValidationTest
6+
{
7+
8+
#region Constants & Statics
9+
10+
public static void ValidationAllow()
11+
{
12+
var product = new Product
13+
{
14+
Price = 100_000.00m,
15+
Price2 = 0.01m,
16+
Price3 = 1000000000000000, // no error, is wrong!
17+
Price4 = 999_999_999_999_999.99981m // no error
18+
};
19+
var context = new ValidationContext(product);
20+
var results = new List<ValidationResult>();
21+
22+
var isValid = Validator.TryValidateObject(product, context, results, true);
23+
24+
if (!isValid)
25+
{
26+
foreach (var error in results)
27+
{
28+
Console.WriteLine($"{string.Join(',', error.MemberNames)} : {error.ErrorMessage}");
29+
}
30+
}
31+
}
32+
33+
public static void ValidationError()
34+
{
35+
var product = new Product
36+
{
37+
Price = -1.00m,
38+
Price2 = 0.001m,
39+
Price3 = 999_999_999_999_999.99991m, // no error, is wrong!
40+
Price4 = 999_999_999_999_999.99991m // error
41+
};
42+
var context = new ValidationContext(product);
43+
var results = new List<ValidationResult>();
44+
45+
var isValid = Validator.TryValidateObject(product, context, results, true);
46+
47+
if (!isValid)
48+
{
49+
foreach (var error in results)
50+
{
51+
Console.WriteLine($"{string.Join(',', error.MemberNames)} : {error.ErrorMessage}");
52+
}
53+
}
54+
}
55+
56+
#endregion
57+
58+
public class Product
59+
{
60+
61+
#region Properties
62+
63+
[Range(0.01, 100_000.00, ErrorMessage = "价格必须在0.01到100,000.00之间")]
64+
public decimal Price { get; set; }
65+
66+
[Range(typeof(decimal), "0.01", "100000.00")]
67+
[DataType(DataType.Currency)]
68+
[DisplayFormat(DataFormatString = "{0:C2}")]
69+
public decimal Price2 { get; set; }
70+
71+
[Range(0.0001, 999_999_999_999_999.9999)] // 超过 double 有效位数,between 0.0001 and 1000000000000000
72+
public decimal Price3 { get; set; }
73+
74+
[Range(typeof(decimal), "0.0001", "999999999999999.9999")]
75+
public decimal Price4 { get; set; }
76+
77+
#endregion
78+
}
79+
}

src/Tests/StructTest/ValueTypeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace StructTest;
22

3-
public class ValueTypeTest
3+
public static class ValueTypeTest
44
{
55

66
#region Constants & Statics

0 commit comments

Comments
 (0)