C# displaying error 'Delegate 'System.Func<...>' does not take 1 arguments
I am calling:
form = new FormFor<Project>()
.Set(x => x.Name, "hi");
where Project has a field called Name and FormFor's code is:
public class FormFor<TEnti开发者_JS百科ty> where TEntity : class
{
FormCollection form;
public FormFor()
{
form = new FormCollection();
}
public FormFor<TEntity> Set(Expression<Func<TEntity>> property, string value)
{
form.Add(property.PropertyName(), value);
return this;
}
}
but it keeps telling me Delegate 'System.Func<ProjectSupport.Core.Domain.Project>' does not take 1 arguments
and I am unsure why. Could anyone shed some light on it for me?
It's trying to convert this lambda expression:
x => x.Name
into an Expression<Func<TEntity>>
.
Let's ignore the expression tree bit for the moment - the delegate type Func<TEntity>
represents a delegate which takes no arguments, and returns a TEntity
. Your lambda expression x => x.Name
clearly is expecting a parameter (x
). I suspect you want
Expression<Func<TEntity, string>>
or something similar, but it's not really clear what you're trying to do.
Type of expression "x => x.Name" is not Expression<Func<TEntity>>
, but Expression<Func<TEntity, string>>
. I suppose, you should change declaration of Set method:
public FormFor<TEntity> Set<V>(Expression<Func<TEntity, V>> property, string value)
Func<TEntity>
is a delegate taking zero parameters and returns an object of type TEntity
. You are trying to supply an x
and return nothing.
精彩评论