Create Attributes
Create new folder named Attributes and create new attributes as below:
Email Attribute
Create new file named Email.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class Email : Attribute
{
}
}
Max Annotation
Create new file named Max.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class Max : Attribute
{
public int Value { get; set; }
}
}
Min Annotation
Create new file named Min.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class Min : Attribute
{
public int Value { get; set; }
}
}
MinLength Annotation
Create new file named MinLength.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MinLength : Attribute
{
public int Value { get; set; }
}
}
MaxLength Annotation
Create new file named MaxLength.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MaxLength : Attribute
{
public int Value { get; set; }
}
}
NotEmpty Annotation
Create new file named NotEmpty.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class NotEmpty : Attribute
{
}
}
URL Annotation
Create new file named URL.cs as below:
using System;
namespace LearnAdvancedCSharpWithRealApps.Attributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class URL : Attribute
{
}
}
Create Entity Class
Create Entities folder and create new class named Account.cs as below:
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; }
}
}
Run Application
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;
}
}
}
}
Structure of Project
Output
Valid: False