What I'm trying to do is generalize making repositories using AutoFixture and Moq. I have a method called 'add' that adds fake records to a list. The list is called records and is global to the class. The generic M is a model to be mocked. The method returns 'this' so the method can be chained.
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;
}
with the extension class (I found this searching 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);
}
}
The error I'm getting is "ArgumentException occurred in Ploeh.AutoFixture.dll".
My question is: How can one pass a property of a generic object in as a parameter on a method when the model is unknown at compile time?
With
method requires Expression<Func<T, TProperty>>
parameter not PropertyInfo
.
You can change your add
method to take Expression<Func<T, TProperty>>
instead of string :
public Repo<M> add<T>(Expression<Func<M, T>> propertyPicker, T val)
{
var a = fixture.Build<M>().With(propertyPicker, val).Create();
records.Add(a);
return this;
}
and this is the usage :
add(foo => foo.Name, "abc");