表达式树:静态方法需要空实例,非静态方法需要非空实例
expr. tree: Static method requires null instance, non-static method requires non-null instance
我搜索了问题并找到了一些主题,我怀疑错误原因,但我无法弄清楚。
我想构建这个表达式部分:
Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex))
我已经用这个构建了 Function(grpindex) row(grpindex)
部分:
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
声明是:
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
现在,我想像这样构建 Select
部分:
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(grpindexes, selectMethod, fieldselector), rowParameter)
声明是:
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Queryable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Object), GetType(System.Func(Of Integer, Object)))
groupedindexes
是正常的 List(Of Integer)
.
在运行时,我在 outerfieldselector=...
行收到上述错误
在我看来,它应该有效。我用一个参数 (fieldselector
).
在 grpindexes
上调用 Select
方法
可能是什么问题?
谢谢。
编辑:示例项目可以在link下载:http://www.filedropper.com/exptree
编辑二:
这里是一个简单、简短的控制台应用程序项目:
Imports System.Reflection
Imports System.Linq.Expressions
Module Module1
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim expr As Expression = Nothing
Dim groupedindexes As New List(Of Integer)
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Queryable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(
Function(m) m.Name = "Select").MakeGenericMethod(GetType(Object), GetType(System.Func(Of Integer, Object)))
Dim fieldselector As Expressions.LambdaExpression
Dim outerfieldselector As Expressions.LambdaExpression
Sub Main()
groupedindexes.Add(0)
groupedindexes.Add(1)
groupedindexes.Add(2)
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
outerfieldselector = Expression.Lambda(Expression.Call(grpindexes, selectMethod, fieldselector), rowParameter)
End Sub
End Module
编辑 3:
我想,我是在svick的帮助下搞定的。
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
问题是 Queryable.Select()
不是实例方法。你称它为 VB 中的一个,但它不是,这反映在表达式树中。
因此,该行应如下所示:
outerfieldselector = Expression.Lambda(Expression.Call(Nothing, selectMethod, grpindexes, fieldselector), rowParameter)
即使您修复了该问题,您的代码仍然无法运行。其中一些问题是:
MakeGenericMethod()
需要类型参数的类型。这里是 TSource
和 TResult
,应该是 Integer
和 Object
。
List(Of Integer)
没有实现 IQueryable
.
我搜索了问题并找到了一些主题,我怀疑错误原因,但我无法弄清楚。
我想构建这个表达式部分:
Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex))
我已经用这个构建了 Function(grpindex) row(grpindex)
部分:
Dim fieldselector As Expressions.LambdaExpression
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
声明是:
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
现在,我想像这样构建 Select
部分:
Dim outerfieldselector As Expressions.LambdaExpression
outerfieldselector = Expression.Lambda(Expression.Call(grpindexes, selectMethod, fieldselector), rowParameter)
声明是:
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Queryable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Object), GetType(System.Func(Of Integer, Object)))
groupedindexes
是正常的 List(Of Integer)
.
在运行时,我在 outerfieldselector=...
在我看来,它应该有效。我用一个参数 (fieldselector
).
grpindexes
上调用 Select
方法
可能是什么问题?
谢谢。
编辑:示例项目可以在link下载:http://www.filedropper.com/exptree
编辑二:
这里是一个简单、简短的控制台应用程序项目:
Imports System.Reflection
Imports System.Linq.Expressions
Module Module1
Dim rowParameter = Expression.Parameter(GetType(Object()), "Row")
Dim indexParameter = Expression.Parameter(GetType(Integer), "grpindex")
Dim expr As Expression = Nothing
Dim groupedindexes As New List(Of Integer)
Dim grpindexes As Expression = Expression.Constant(groupedindexes, GetType(System.Collections.Generic.List(Of Integer)))
Dim selectMethod = GetType(Queryable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(
Function(m) m.Name = "Select").MakeGenericMethod(GetType(Object), GetType(System.Func(Of Integer, Object)))
Dim fieldselector As Expressions.LambdaExpression
Dim outerfieldselector As Expressions.LambdaExpression
Sub Main()
groupedindexes.Add(0)
groupedindexes.Add(1)
groupedindexes.Add(2)
fieldselector = Expression.Lambda(Expression.ArrayAccess(rowParameter, indexParameter), indexParameter)
outerfieldselector = Expression.Lambda(Expression.Call(grpindexes, selectMethod, fieldselector), rowParameter)
End Sub
End Module
编辑 3:
我想,我是在svick的帮助下搞定的。
Dim selectMethod = GetType(Enumerable).GetMethods(BindingFlags.Public Or BindingFlags.Static).First(Function(m) m.Name = "Select").MakeGenericMethod(GetType(Integer), GetType(Object))
问题是 Queryable.Select()
不是实例方法。你称它为 VB 中的一个,但它不是,这反映在表达式树中。
因此,该行应如下所示:
outerfieldselector = Expression.Lambda(Expression.Call(Nothing, selectMethod, grpindexes, fieldselector), rowParameter)
即使您修复了该问题,您的代码仍然无法运行。其中一些问题是:
MakeGenericMethod()
需要类型参数的类型。这里是TSource
和TResult
,应该是Integer
和Object
。List(Of Integer)
没有实现IQueryable
.