创建属性
创建名为 "属性"的新文件夹并创建新的属性,如下所示。
电子邮件属性
创建名为Email.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class Email : Attribute
{
}
}
Max注释
创建名为Max.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class Max : Attribute
{
public int Value { get; set; }
}
}
最小注解
创建名为Min.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class Min : Attribute
{
public int Value { get; set; }
}
}
MinLength注解
创建名为MinLength.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MinLength : Attribute
{
public int Value { get; set; }
}
}
MaxLength注解
创建名为MaxLength.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MaxLength : Attribute
{
public int Value { get; set; }
}
}
NotEmpty注解
创建名为NotEmpty.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class NotEmpty : Attribute
{
}
}
URL注解
创建名为URL.cs的新文件,如下所示。
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class URL : Attribute
{
}
}
创建实体类
创建Entities文件夹,并创建名为Account.cs的新类,如下所示。
using LearnAdvancedCSharpWithRealApps.Attributes;
namespace LearnAdvancedCSharpWithRealApps.Entities
{
public class Account
{
[NotEmpty]
[MinLength(Value = 3)]
[MaxLength(Value = 10)]
public string Username { get; set; }
[NotEmpty]
[Email]
public string Email { get; set; }
[Min(Value = 18)]
[Max(Value = 60)]
public int Age { get; set; }
[URL]
public string Website { get; set; }
}
}
运行应用程序
using LearnAdvancedCSharpWithRealApps.Attributes;
using LearnAdvancedCSharpWithRealApps.Entities;
using System;
using System.Reflection;
namespace LearnAdvancedCSharpWithRealApps
{
class Program
{
static void Main(string[] args)
{
var account = new Account
{
Username = "abca",
Age = 22,
Email = "aa@gmail.com",
Website = "https://learningprogramming.net"
};
Console.WriteLine("Valid: " + isValid(account));
Console.ReadLine();
}
private static bool isValid(object obj)
{
bool result = true;
try
{
foreach (var propertyInfo in obj.GetType().GetProperties())
{
foreach (object ob in propertyInfo.GetCustomAttributes())
{
if (ob is NotEmpty)
{
var value = propertyInfo.GetValue(obj).ToString();
if (string.IsNullOrEmpty(value))
{
result = false;
}
}
if (ob is MinLength)
{
var minLength = ob as MinLength;
var value = propertyInfo.GetValue(obj).ToString();
if (value.Length < minLength.Value)
{
result = false;
}
}
if (ob is MaxLength)
{
var maxLength = ob as MaxLength;
var value = propertyInfo.GetValue(obj).ToString();
if (value.Length > maxLength.Value)
{
result = false;
}
}
if (ob is Min)
{
var min = ob as Min;
var value = int.Parse(propertyInfo.GetValue(obj).ToString());
if (value < min.Value)
{
result = false;
}
}
if (ob is Max)
{
var max = ob as Max;
var value = int.Parse(propertyInfo.GetValue(obj).ToString());
if (value > max.Value)
{
result = false;
}
}
if (ob is Email)
{
var value = propertyInfo.GetValue(obj).ToString();
if (!IsValidEmail(value))
{
result = false;
}
}
if (ob is URL)
{
var value = propertyInfo.GetValue(obj).ToString();
if (!IsValidURL(value))
{
result = false;
}
}
}
}
}
catch
{
result = false;
}
return result;
}
private static bool IsValidURL(string url)
{
Uri uriResult;
return Uri.TryCreate(url, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp;
}
private static bool IsValidEmail(string email)
{
try
{
var addr = new System.Net.Mail.MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
}
}
项目的结构

输出
Valid: False