我正在尝试做的是概括使用 AutoFixture 和 Moq 制作存储库。我有一个名为“添加”的方法,可以将虚假记录添加到列表中。该列表称为记录,对类是全局的。通用 M 是要模拟的模型。该方法返回“this”,因此可以链接该方法。
public Repo<M> add(string prop, string val) {
var a = fixture.Build<M>().With(m => m.GetProperty(prop), val).Create();
records.Add(a);
return this;
}
使用扩展类(我发现这个搜索 SO):
public static class MyExtensions
{
public static object GetProperty<T>(this T obj, string name) where T : class
{
Type t = typeof(T);
return t.GetProperty(name).GetValue(obj, null);
}
}
我得到的错误是“Ploeh.AutoFixture.dll 中发生 ArgumentException”。
我的问题是:当模型在编译时未知时,如何将通用对象的属性作为参数传递给方法?