Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Extension Methods not accessible with aliased using directive

The following code compiles:

using Microsoft.SharePoint.Client

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

However, when switching to an aliased using, there is a problem with the Include function, which is an extension method:

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

The Include function can still be used, but not as an extension method. Is there any way to use it as an extension method without un-aliasing the using directive?

like image 524
Hutch Avatar asked Jul 20 '15 20:07

Hutch


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


1 Answers

Not before C# 6... but in C# 6 you can use:

using static Microsoft.SharePoint.Client.ClientObjectQueryableExtension;

using SP = Microsoft.SharePoint.Client;

The first of these will pull in just the static members of ClientObjectQueryableExtension available, without anything to do with the rest of the namespace.

like image 148
Jon Skeet Avatar answered Sep 21 '22 19:09

Jon Skeet