创建自定义属性时,会创建一个从 ValidationAttribute 类扩展而来的自定义类。
public class CustomAttribute: ValidationAttribute
{
public void Test()
{
Console.WriteLine("Hello World");
}
public override bool IsValid(object? value)
{
//Logic here
if (condition)
{
return true;
}
else
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
假设此自定义属性放置在模型的属性之上。
[Custom]
public string username { get; set; }
Run Code Online (Sandbox Code Playgroud)
通过将自定义放置在属性上,我实例化了自定义类。现在我的查询是