Carl Onager
Carl Onager

Reputation: 4122

How can I duplicate VB.NET 'Import Namespace' functionality in a C# project?

In a VB.NET project, on the Add Reference screen it is possible to 'Import Namespaces'. Namespaces added like this are globally declared so that you do not have to add an Import Namespace line to each file.

This allows, in rare cases, a VB developer to share/link one code file to two projects and have a different namespace in each project resolve to the same classes in a third project. This solves a particular problem I have with references to WCF services via other WCF services that is caused by our architecture.

I now have to use this code, suitably translated, in a C# project and I couldn't find a way to add a namespace at a global level because in C# the using directive, like the Import statement applies only to the file it is in.

Is it possible to duplicate the 'Import Namespaces' functionality of a VB.NET project in a C# project?

Upvotes: 1

Views: 249

Answers (1)

Roy Dictus
Roy Dictus

Reputation: 33139

Only in ASP.NET can you do that. There you can do the global imports using the web.config file, like so:

<configuration>
 <system.web>
    <pages>
      <namespaces>
        <add namespace="System.Data.Linq"/>
        <add namespace="System.IO"/>
        <add namespace="System.Text"/>
      </namespaces>
    </pages>  
  </system.web>
</configuration>

Upvotes: 2

Related Questions