2010-12-17 73 views
2

我有一个类,有没有办法用这个随机数据从这个类快速生成很多对象?如何生成测试数据?

+0

相关:https://stackoverflow.com/questions/6625490/c-sharp-library-to-populate-object-with-random-data和https://stackoverflow.com/questions/1413342/are-there - 任何工具到填入类 - 属性 - 使用随机数据 – MatthewMartin 2015-01-06 18:35:37

回答

3

用户反射通过所有属性并为其设置随机值。 somethig这样

object classObject; 
PropertyInfo[] propertyInfos; 
propertyInfos = typeof(classObject).GetProperties(BindingFlags.Public | BindingFlags.Static); 
foreach (PropertyInfo propertyInfo in propertyInfos) 
{ 
    propertyInfo.SetValue(classObject, value, null) 
} 
8

可以使用NBuilder

通过流畅的,可扩展的接口生成测试数据,NBuilder可以让你快速创建测试数据,对那些类型的属性和公共领域的自动分配值内置的.NET数据类型(例如整数和字符串)。