当前位置:威尼斯wns.9778官网活动 > 计算机教程 > 对System.ComponentModel.DataAnnotations 的学习应用
对System.ComponentModel.DataAnnotations 的学习应用
文章作者:计算机教程 上传时间:2019-05-10
1如果如图所示使用多重继承,我们将看到什么
Demo下载地址:http://download.csdn.net/detail/u014265946/9330181
3代码验证吧
class BaseClass:
num_base_calls = 0
def call_me(self):
print("Calling method on Based Class")
self.num_base_calls = 1
class LeftSubclass(BaseClass):
num_left_calls = 0
def call_me(self):
BaseClass.call_me(self)
print("Calling method on Left Subclass")
self.num_left_calls = 1
class RightSubclass(BaseClass):
num_right_calls = 0
def call_me(self):
BaseClass.call_me(self)
print("Calling method on Right Subclass")
self.num_right_calls = 1
class Subclass(LeftSubclass, RightSubclass):
num_sub_calls = 0
def call_me(self):
LeftSubclass.call_me(self)
RightSubclass.call_me(self)
print("Calling method on Subclass")
self.num_sub_calls = 1
s = Subclass()
s.call_me()
print(s.num_sub_calls, s.num_left_calls, s.num_right_calls, s.num_base_calls)
威尼斯wns.9778官网活动,
4改进措施
2我们看到了基类被执行了两次Baseclass
5完美解决=基类执行了一次
6代码验证
class BaseClass:
num_base_calls = 0
def call_me(self):
print("Calling method on Based Class")
self.num_base_calls = 1
class LeftSubclass(BaseClass):
num_left_calls = 0
def call_me(self):
# BaseClass.call_me(self)
super().call_me()
print("Calling method on Left Subclass")
self.num_left_calls = 1
class RightSubclass(BaseClass):
num_right_calls = 0
def call_me(self):
# BaseClass.call_me(self)
super().call_me()
print("Calling method on Right Subclass")
self.num_right_calls = 1
class Subclass(LeftSubclass, RightSubclass):
num_sub_calls = 0
def call_me(self):
# LeftSubclass.call_me(self)
# RightSubclass.call_me(self)
super().call_me()
print("Calling method on Subclass")
self.num_sub_calls = 1
s = Subclass()
s.call_me()
print(s.num_sub_calls, s.num_left_calls, s.num_right_calls, s.num_base_calls)
参考:本文参考学习《Python3 Object Oriented Programming》,根据自己理解改编,Dusty Phillips 著
public static class ExtensionHelper
{
/// <summary>
/// 验证对象是否有效
/// </summary>
/// <param name="obj">要验证的对象</param>
/// <param name="validationResults"></param>
/// <returns></returns>
public static bool IsValid(this object obj, Collection<ValidationResult> validationResults)
{
return Validator.TryValidateObject(obj, new ValidationContext(obj, null, null), validationResults, true);
}
/// <summary>
/// 验证对象是否有效
/// </summary>
/// <param name="obj">要验证的对象</param>
/// <returns></returns>
public static bool IsValid(this object obj)
{
return Validator.TryValidateObject(obj, new ValidationContext(obj, null, null), new Collection<ValidationResult>(), true);
}
}
最近写C#项目中的时候,在验证数据的有效性的时候写了很多判断,结果工作量很大,然后就想能实现在类属性上标示验证的特性,来验证数据的有效性,以前听说过,但是从来没有实现过,也很少看到在项目中别人使用过,所以就一直没有研究过,但是最近在写Model的时候需要验证很多数据的有效性,所以就想研究一下。
这里定义了一个验证特性,主要是来标示属性的最大长度,和当大于最大长度是得提示信息。
/// <summary>
/// 数据模型
/// </summary>
public class DataModel : MyIsValid<DataModel>
{
/// <summary>
/// 值
/// </summary>
[StringLength(5, ErrorMessage = "Value最大长度为5")]
public string Value { get; set; }
}
class Program
{
static void Main(string[] args)
{
DataModel r = new DataModel();
r.EmailAddress = "cdaimesdfng1m";
var v = new Collection<ValidationResult>();
if (r.IsValid(v))
{
Console.WriteLine("1");
}
else
{
v.ToList().ForEach(e =>
{
Console.WriteLine(e.ErrorMessage);
});
}
Console.ReadKey();
}
}
public class DataModel
{
/// <summary>
///
/// </summary>
[Required]
[StringLength(5, ErrorMessage = "太大")]
public string Name { get; set; }
/// <summary>
///
/// </summary>
[Range(2, 5)]
public string d { get; set; }
/// <summary>
///
/// </summary>
[EmailAddress]
public string EmailAddress { get; set; }
}
然后我就写了一个基类,统统的在基类中做验证。下面是基类的代码,打算以后所有的需要做验证的类,都继承该基类,将属性标识上特性做验证呢(后来发现更好的办法),写的不好,还请多多指教。
摘要
public class MyIsValid<T> where T : class
{
//验证信息
internal string Msg { get; set; }
// 验证是否有效
internal bool IsValid()
{
var v = this as T;
Type type = v.GetType();
PropertyInfo[] propeties = type.GetProperties();
foreach (PropertyInfo property in propeties)
{
List<Attribute> attributes = property.GetCustomAttributes().ToList();
var propertyValue = property.GetValue(v);
Attribute stringlength = attributes.FirstOrDefault(p => p.GetType().IsAssignableFrom(typeof(StringLengthAttribute)));
if (stringlength == null)
continue;
int length = ((StringLengthAttribute)stringlength).MaximumLength;
string currentValue = (string)propertyValue;
if (currentValue.Length > length)
{
Msg = ((StringLengthAttribute)stringlength).ErrorMessage;
return false;
}
}
return true;
}
}
本文由威尼斯wns.9778官网活动发布于计算机教程,转载请注明出处:对System.ComponentModel.DataAnnotations 的学习应用
关键词:
下一篇:没有了