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

时间:2013-01-11 21:32:14

标签: c# sql linq

我从博客scottgu学习Linq到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;
    }
}

1 个答案:

答案 0 :(得分:0)

DataContext.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 });