简体   繁体   中英

Why does fully qualified C# import fail?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM