Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可枚举类型属性的元素属性的路径表达式_C#_Expression - Fatal编程技术网

C# 可枚举类型属性的元素属性的路径表达式

C# 可枚举类型属性的元素属性的路径表达式,c#,expression,C#,Expression,我正在编写一个方法,它需要知道从源对象到嵌套属性的路径。我在考虑做一个表情。我的方法签名如下所示 public bool DetailsQueried<T>(IResolverContext ctx, Expression<Func<T,object>>propertyPath) 我使用它如下 Analyzer.DetailsQueried<PaginatedRecords>(ctx, page => page.PageCount) 现在

我正在编写一个方法,它需要知道从源对象到嵌套属性的路径。我在考虑做一个表情。我的方法签名如下所示

public bool DetailsQueried<T>(IResolverContext ctx, Expression<Func<T,object>>propertyPath)
我使用它如下

Analyzer.DetailsQueried<PaginatedRecords>(ctx, page => page.PageCount)
现在,我想导航到一个嵌套属性,它是源类型上可枚举属性项的一部分

Analyzer.DetailsQueried<PaginatedRecords>(ctx, page => page.Records[0].Name)

正如您在上面看到的,上面的索引0是不相关的,我只是想指定Name属性。最好的方法是什么?

在我看来,您可以在这里使用Fluen API。 例如,您可以使用

对于您的具体情况,可以是:

public class Analyzer<T>
{
    public bool DetailsQueried(IResolverContext ctx, Expression<Func<T, object>> simpleProperty) => true;
    public Analyzer<TItem> DetailsQueried<TItem>(IResolverContext ctx, Expression<Func<T, ICollection<TItem>>> simpleProperty) => new Analyzer<TItem>();
}
用作:

var analyzer = new Analyzer<PaginatedRecords>();
analyzer
    .DetailsQueried(ctx, outer => outer.Records)
    .DetailsQueried(ctx, inner => inner.Name);

到目前为止你尝试了什么?为什么索引不相关?索引0处的名称可能与索引1处的名称不同。您在DetailsCreed中实际做了什么?你还了一本书?为什么?你为什么要在最后一行中使用Analyzer.detailsquered而不是Analyzer.detailsquered?@Sweeper很抱歉,从我描述的方式来看,这并不明显。我正在编写一个方法来检查graphql查询,如果查询了对象的某些嵌套属性。因此,在我的用例中,索引是不相关的?