ERROR静态方法需要null实例,非静态方法需要非null实例 [英] ERROR Static method requires null instance, non-static method requires non-null instance

查看:837
本文介绍了ERROR静态方法需要null实例,非静态方法需要非null实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个表达式树。我需要从数据表中读取数据并检查其列。要检查的列以及要检查的列数在运行时仅已知。列名称作为字符串数组给我,每个列都有一个要检查的字符串列表。



这里我遇到一个错误。



strong>静态方法需要null实例,非静态方法需要非null实例。
参数名称:实例





inner = Expression.Call(rowexp,mi,colexp);



请帮我!!!。

  IQueryable< DataRow> queryableData = CapacityTable.AsEnumerable()。AsQueryable()。Where(row2 => values.Contains(row2.Field< string>(Head1)。ToString())&& string>(Head2)。ToString())); 


MethodInfo mi = typeof(DataRowExtensions).GetMethod(Field,new Type [] {typeof(DataRow),typeof(string)});
mi = m.MakeGenericMethod(typeof(string));

ParameterExpression rowexp = Expression.Parameter(typeof(DataRow),row);
ParameterExpression valuesexp = Expression.Parameter(typeof(List< string>),values);
ParameterExpression fexp = Expression.Parameter(typeof(List< string>),types);
表达式inner,outer,predicateBody = null;


foreach(类型中的var col)
{
// DataRow row = CapacityTable.Rows [1];

ParameterExpression colexp = Expression.Parameter(typeof(string),col);
//表达式left = Expression.Call(pe,typeof(string).GetMethod(ToLower,System.Type.EmptyTypes));

inner = Expression.Call(rowexp,mi,colexp);


outer = Expression.Call(valuesexp,typeof(List< string>)。GetMethod(Contains),inner);


predicateBody = Expression.And(predicateBody,outer);

}

MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
Where,
new Type [] { queryableData.ElementType},
queryableData.Expression,
Expression.Lambda< Func< DataRow,bool>>(predicateBody,new ParameterExpression [] {rowexp}))


解决方案

这意味着您试图表示的方法调用是一个静态的,但你给它一个目标表达式。这就像试图调用:

  Thread t = new Thread(...) 
//无效!
t.Sleep(1000);

你试图在表达式树形式中这样做,这是不允许的。



看起来这是发生在 Field 扩展方法 DataRowExtensions - 因此,扩展方法的目标需要表示为调用的第一个参数,因为您实际想调用:

  DataRowExtensions.Field< T>(row,col); 

 >  inner = Expression.Call(mi,rowexp,colexp); 

这将调用这是重载,这是调用具有两个参数的静态方法的方式。


I am trying to create an expression tree. I need to read data from a data table and check its columns. The columns to be checked and also the number of columns to be checked are known at run time only. The column names are given to me as a string array and and each column has a List of strings to be checked. I tried out sample expression trees , like the one below.

Here I am encountering an error.

Static method requires null instance, non-static method requires non-null instance. Parameter name: instance

at the line

inner = Expression.Call(rowexp,mi, colexp);

Kindly help me out!!!

          IQueryable<DataRow> queryableData = CapacityTable.AsEnumerable().AsQueryable().Where(row2 => values.Contains(row2.Field<string>("Head1").ToString()) && values.Contains(row2.Field<string>("Head2").ToString()));


        MethodInfo mi = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow),typeof(string) });
        mi = mi.MakeGenericMethod(typeof(string));

            ParameterExpression rowexp = Expression.Parameter(typeof(DataRow), "row");
            ParameterExpression valuesexp = Expression.Parameter(typeof(List<string>), "values");
            ParameterExpression fexp = Expression.Parameter(typeof(List<string>), "types");
            Expression inner,outer,predicateBody=null;


            foreach (var col in types)
            {
               // DataRow row = CapacityTable.Rows[1];

                ParameterExpression colexp = Expression.Parameter(typeof(string), "col");
              //  Expression left = Expression.Call(pe, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));

                inner = Expression.Call(rowexp,mi, colexp);


                outer = Expression.Call(valuesexp, typeof(List<string>).GetMethod("Contains"), inner);


                predicateBody = Expression.And(predicateBody,outer);

            }

            MethodCallExpression whereCallExpression = Expression.Call(
                typeof(Queryable),
                "Where",
                new Type[] { queryableData.ElementType },
                queryableData.Expression,
                Expression.Lambda<Func<DataRow,bool>>(predicateBody, new ParameterExpression[] { rowexp }));

解决方案

It means the method call you're trying to represent is a static one, but you're giving it a target expression. That's like trying to call:

Thread t = new Thread(...);
// Invalid!
t.Sleep(1000);

You're sort of trying to do that in expression tree form, which isn't allowed either.

It looks like this is happening for the Field extension method on DataRowExtensions - so the "target" of the extension method needs to be expressed as the first argument to the call, because you actually want to call:

DataRowExtensions.Field<T>(row, col);

So you want:

inner = Expression.Call(mi, rowexp, colexp);

That will call this overload which is the way to call a static method with two arguments.

这篇关于ERROR静态方法需要null实例,非静态方法需要非null实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆