Link to home
Start Free TrialLog in
Avatar of endrec
endrec

asked on

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
Avatar of Jini Jose
Jini Jose
Flag of India image

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.
ASKER CERTIFIED SOLUTION
Avatar of Jini Jose
Jini Jose
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of endrec
endrec

ASKER

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]"?
yes exactly..
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 );
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 );
Avatar of endrec

ASKER

Will this actually change the namespace in the physical .cs file?
No.
The alias applies only within the program you write. The physical phile containing the namespace declaration is unchanged.