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/Cla ssWithoutN amespace .cs (e.g. C:\Projects\Test\Entities\ ClassWitho utNamespac e .cs)
public class ClassWithoutNamespace : ISomething
{
public string Something { get; set; }
}
{projectPath}/Interfaces/I Something. cs (e.g. C:\Projects\Test\Interface s\ISomethi ng.cs)
public interface ISomething
{
}
Examples of New Files
C:\Projects\TestNew\Entiti es\ClassWi thoutNames pace .cs)
namespace ProjectName.Entities
public class ClassWithoutNamespace : ISomething
{
public string Someting { get; set; }
}
end namespace
C:\Projects\TestNew\Interf aces\ISome thing.cs
namespace ProjectName.Interfaces
public interface ISomething
{
}
end namespace
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/Cla
public class ClassWithoutNamespace : ISomething
{
public string Something { get; set; }
}
{projectPath}/Interfaces/I
public interface ISomething
{
}
Examples of New Files
C:\Projects\TestNew\Entiti
namespace ProjectName.Entities
public class ClassWithoutNamespace : ISomething
{
public string Someting { get; set; }
}
end namespace
C:\Projects\TestNew\Interf
namespace ProjectName.Interfaces
public interface ISomething
{
}
end namespace
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 );
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 );
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 );
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.
The alias applies only within the program you write. The physical phile containing the namespace declaration is unchanged.
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.