Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does fully qualified C# import fail?

Tags:

c#

.net

In my main method, I had a line of code -

WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName);

I added an import - System.Net.WebRequest. This causes an error:

The type or namespace name 'YourProject' could not be found (are you missing a using directive or an assembly reference?).

The error goes away when I change the import to System.Net Why does this happen ?

Actually, I am using this code inside a C# script task of SSIS, an ETL tool. I mention that in case you cannot replicate the problem in plain visual studio and want to declare SSIS as a source of the problem.

like image 584
Steam Avatar asked Jan 11 '23 13:01

Steam


1 Answers

using Directive (C# Reference)

The using directive has two uses:

  • To allow the use of types in a namespace so that you do not have to qualify the use of a type in that namespace:

using System.Text;

  • To create an alias for a namespace or a type. This is called a using alias directive.

using Project = PC.MyCompany.Project;

You can't import a type, you have to import the entire namespace, and that's why using System.New.WebRequest does no work, and your code does not compile.

You could create an alias to WebRequest, to prevent entire namespace to be imported, but I personally don't like that solution:

using WebRequest = System.Net.WebRequest;

And yes, the error could be a little more descriptive, but that's the problem here.

like image 112
MarcinJuraszek Avatar answered Jan 17 '23 15:01

MarcinJuraszek