C#:使用别名使用指令无法访问的扩展方法

Hut*_*tch 7 c# extension-methods

以下代码编译:

using Microsoft.SharePoint.Client

class Dummy()
{
    void DummyFunction(ClientContext ctx, ListCollection lists)
    {
        Context.Load(lists, lc => lc.Include(l => l.DefaultViewUrl);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当切换到使用别名时,Include函数存在问题,这是一种扩展方法:

using SP = Microsoft.SharePoint.Client

class DummyAliased()
{
    void DummyFunction(SP.ClientContext ctx, SP.ListCollection lists)
    {
        /* Does not compile: */
        Context.Load(lists, lc => lc.Include(l => l.DefaultViewUrl);
        /* Compiles (not using function as generic) */
        Context.Load(lists, lc => SP.ClientObjectQueryableExtension.Include(lc, l => l.DefaultViewUrl));
    }
}
Run Code Online (Sandbox Code Playgroud)

仍然可以使用Include函数,但不能作为扩展方法.有没有办法将它用作扩展方法而不取消使用using指令?

Jon*_*eet 11

不是在C#6之前......但是在C#6中你可以使用:

using static Microsoft.SharePoint.Client.ClientObjectQueryableExtension;

using SP = Microsoft.SharePoint.Client;
Run Code Online (Sandbox Code Playgroud)

第一个将ClientObjectQueryableExtension引入可用的静态成员,而不与命名空间的其余部分有任何关系.