我正在使用表达式。是否有可能有一个genric返回类型,所以我想返回属性传递到我的表达式中的任何类型。
public PropertyMapping(Expression<Func<TEntity, int>> expression)
{
this.expression = expression;
if (this.expression != null)
{
this.expressionMemberName =((MemberExpression)this.expression.Body).Member.Name;
}
}
我不想创建StringPropertyMapping,IntPropertyMapping,DoublePropertyMapping等....
答案 0 :(得分:0)
您需要为PropertyMapping
类添加属性类型的类型参数。如果它嵌套在定义TEntity
类型参数的类中,它将变为PropertyMapping<TProperty>
:
public class SomeClass<TEntity>
{
public class PropertyMapping<TProperty>
{
public PropertyMapping(Expression<Func<TEntity, TProperty>> expression)
{
...
如果是顶级课程,则会成为PropertyMapping<TEntity, TProperty>
:
public class PropertyMapping<TEntity, TProperty>
{
public PropertyMapping(Expression<Func<TEntity, TProperty>> expression)
{
...