• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

How to programmatically add/change namespaces of classes based on folder location?

Hello,
I'm having an issue where I need to upgrade a project and previously (it was a .NET 2.0 ASP.NET web site, with no project) namespaces were not specified on all the code files, which seems to prevent the web site from building as a "Web Application" in .NET 4.0 (well anything after 2.0).

I wanted to find out if there is a way that I could iterate through all files ending in .cs in the project directory, and if that file contains "public partial class", "public class", "public interface", or "public enum" then add a namespace based on the folder the file is in.  If the file is not in a folder then the namespace would just be ProjectName.

The classes have using/imports statements and some of the .cs files have multiple classes in them.


Examples of Original Files
{projectPath}/Entities/ClassWithoutNamespace .cs (e.g. C:\Projects\Test\Entities\ClassWithoutNamespace .cs)

public class ClassWithoutNamespace : ISomething
{
     public string Something { get; set; }
}

{projectPath}/Interfaces/ISomething.cs (e.g. C:\Projects\Test\Interfaces\ISomething.cs)

public interface ISomething
{
     
}


Examples of New Files
C:\Projects\TestNew\Entities\ClassWithoutNamespace .cs)

namespace ProjectName.Entities
public class ClassWithoutNamespace : ISomething
{
     public string Someting { get; set; }
}
end namespace

C:\Projects\TestNew\Interfaces\ISomething.cs

namespace ProjectName.Interfaces
public interface ISomething
{
}
end namespace
0
endrec
Asked:
endrec
  • 3
  • 3
  • 2
1 Solution
 
Jini Jose.Net Team LeadCommented:
you can do this.
create a new project and loop through the files in that folder.
then use a text writing code you can simply replace the old code to new code easily.
0
 
Jini Jose.Net Team LeadCommented:
0
 
endrecAuthor Commented:
So I would write a RegEx that first checks if the file contains "namespace" and if not run another conditional statement that checks if it contains "public partial class", "public class", "public interface", or "public enum" and replace that with "namespace [Value] [new line]"?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Jini Jose.Net Team LeadCommented:
yes exactly..
0
 
OrcbighterCommented:
Just use aliasing
You can assign an alias to a namespace
for example:

using System;

namespace Blargle
{
   class MyPerfectClass
   {
      ...
   }
}

// now, in the code


using System;
using Blargle;

...
   MyPerfectClass inst = new MyPerfectClass();
   int.Add( stuff );

// now to programmatically rename that namespace  

using System;
using anAlias = Blargle;

     Blargle::MyPerfectclass inst = new Blargle::MyPerfectclas();
    int.Add( stuff );
0
 
OrcbighterCommented:
oops! just fix that typo

Just use aliasing
You can assign an alias to a namespace
for example:

using System;

namespace Blargle
{
   class MyPerfectClass
   {
      ...
   }
}

// now, in the code


using System;
using Blargle;

...
   MyPerfectClass inst = new MyPerfectClass();
   int.Add( stuff );

// now to programmatically rename that namespace  

using System;
using anAlias = Blargle;

     AnAlias::MyPerfectclass inst = new AnAlias::MyPerfectclas();
    int.Add( stuff );
0
 
endrecAuthor Commented:
Will this actually change the namespace in the physical .cs file?
0
 
OrcbighterCommented:
No.
The alias applies only within the program you write. The physical phile containing the namespace declaration is unchanged.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now