jStaff
jStaff

Reputation: 710

How to call specific namespace's function in C#

Currently I have two function from the same library that could both be called in my situation. How do I specify the specific namespace of the function so that it is called.

The call is ambiguous between the following methods or properties: 'Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseContentRoot(Microsoft.AspNetCore.Hosting.IWebHostBuilder, string)' and 'Microsoft.AspNetCore.Hosting.HostingAbstractionsWebHostBuilderExtensions.UseContentRoot(Microsoft.AspNetCore.Hosting.IWebHostBuilder, string)'

That is the error( a bit messy to read ) but that is the problem.

Here is the code:

 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Builder;     
 public static void Main(string[] args)
        {
            var host = new  WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory()) //The Problem!!
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }

Upvotes: 3

Views: 936

Answers (1)

DWright
DWright

Reputation: 9500

You're using a fluent notation, (successive calls to methods chained together), and relying on static extension methods along the way, all of which is nice style. Not sure how to preserve that nice style, but if you pull it apart, you can target the method call you want, via namespace.

 var intermediateResult = new  WebHostBuilder()
    .UseKestrel();

Now choose which method you want (my example uses the method from the first namespace, but it's your choice).

Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseContentRoot(intermediateResult, Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

Ugly, but it should compile, and, presumably, run.


Note. You'll notice that I called the method UseContentRoot() with two parameters! That's merely making explicit what is going on underneath.

Static extension methods are written with an initial parameter representing the class for which the method is going to serve as an extension method. So UseContentRoot(String currentDir) is actually written something like UseContentRoot(this IWebHostBuilder builder, String currentDir), meaning that UseContentRoot is an extension method being written for class (interface) IWebHostBuilder.

The special this keyword usage now allows the method to be called (and chained in your case) as though the method were a member of IWebHostBuilder, so that if you have a IWebHostBuilder builder, you can do builder.UseContentRoot(currentDir). So the initial parameter "moves" to the left of the dot and it looks like this extension method is declared on a IWebHostBuilder as a method taking one parameter.

But calling it as though it were a method belonging to a IWebHostBuilder is just a convenience. You can still call the method as it was originally written, with both of its declared parameters: UseContentRoot(IWebHostBuilder builder, String currentDir), which is how it is actually declared.

Upvotes: 3

Related Questions