我有以下两个Entity Framework的Include方法:
public static IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
[NotNullAttribute] this IQueryable<TEntity> source,
[NotNullAttribute] Expression<Func<TEntity, TProperty>> navigationPropertyPath)
where TEntity : class;
public static IQueryable<TEntity> Include<TEntity>(
[NotNullAttribute] this IQueryable<TEntity> source,
[NotNullAttribute][NotParameterized] string navigationPropertyPath)
where TEntity : class;
我需要获取两个方法的MethodInfo。对于第一个方法,我使用了以下代码:
MethodInfo include1 = typeof(EntityFrameworkQueryableExtensions)
.GetMethods().First(x => x.Name == "Include" && x.GetParameters()
.Select(y => y.ParameterType.GetGenericTypeDefinition())
.SequenceEqual(new[] { typeof(IQueryable<>), typeof(Expression<>) }));
这个可以正常工作,但是当我尝试使用以下方法获取第二个时:
MethodInfo include2 = typeof(EntityFrameworkQueryableExtensions)
.GetMethods().First(x => x.Name == "Include" && x.GetParameters()
.Select(y => y.ParameterType.GetGenericTypeDefinition())
.SequenceEqual(new[] { typeof(IQueryable<>), typeof(String) }));
我遇到了以下错误:
此操作仅适用于泛型类型
我缺少了什么吗?
string navigationPropertyPath
是泛型吗? - CodeCasterParameterType.GetGenericTypeDefinition()
如何返回typeof(String)
? - Lasse V. Karlsen