无法解析符号ExecuteMethodCall (linq to sql)

本文关键字:linq to sql ExecuteMethodCall 符号 | 更新日期: 2023-09-27 17:51:20

我从博客scottgu学习Linq to sql,我得到错误消息:

"无法解析符号ExecuteMethodCall"。

方法ExecuteMethodCallis是linq to sql支持的,但是为什么出现这个错误?

ALTER PROCEDURE dbo.VariableShapeSample
        (
    @shape int 
    )
AS
    if(@shape=1)
    select * from products
    else if (@shape=2)
    select * from orders

public partial class NorthwindDataContext
{
    [Function(Name = "VariableShapeSample")]
    [ResultType(typeof (Product))]
    [ResultType(typeof (Order))]
    public IMultipleResults VariableShapeSample(System.Nullable<int> shape )
    {
        IExecuteResult result = this.ExecuteMethodCall(this
                                                       , ((MethodInfo) (MethodInfo.GetCurrentMethod()))
                                                       , shape);
        return (IMultipleResults) result.ReturnValue;
    }
}

无法解析符号ExecuteMethodCall (linq to sql)

数据上下文。ExecuteMethodCall方法是一个内部方法。你不能叫它。它只能在System.Data.Linq程序集中的文件中访问。你可以用反射来调用它,如果你真的需要的话:

Type type = typeof(NorthwindDataContext);
var methodInfo = type.GetMethod("ExecuteMethodCall", 
                                 BindingFlags.NonPublic | BindingFlags.Instance);
var currentMethod = ((MethodInfo) (MethodInfo.GetCurrentMethod()));
var result = (IExecuteResult)methodInfo.Invoke(this, 
                                 new object[] { this, currentMethod, shape });