3

I would like to combine multiple c# files into one file that will compile without user correction. This is for some online contests that will only accept 1 single file for solutions. This would need to merge all the usings, namespaces and classes.

As an Example, if I had 2 files MyThing.cs and GenericPair.cs I would like to merge them onto a single file, Merged.cs.

A Visual Studio plugin would be ideal but running a command line tool against a project directory is good if it gets the job done.

MyThing.cs

using System.Drawing;
namespace test
{
    public class Thing
    {
        public Point Position;

        public Thing(Point p)
        {
            Position = p;
        }
    }
}

GenericPair.cs

using System;
namespace test2ndNamespace
{
    public class GenericPair<T> where T : IComparable
    {
        public T P;
        public T Q;
        public GenericPair(T p, T q)
        {
            P = p;
            Q = q;
        }
    }
}

Merged.cs

using System;
using System.Drawing;
namespace test
{
    public class Thing
    {
        public Point Position;

        public Thing(Point p)
        {
            Position = p;
        }
    }
}

namespace test2ndNamespace
{
    public class GenericPair<T> where T : IComparable
    {
        public T P;
        public T Q;
        public GenericPair(T p, T q)
        {
            P = p;
            Q = q;
        }
    }
}
1

Here is a quick 1 hour implementation of the software I was looking for.

It is in rough shape, error prone and not a turnkey solution. You will need to copy the class provided at the GitHub link and build an executable to use in your build process. This should be straight forward and only a few project references need to be added.

https://gist.github.com/GER-NaN/a1998db21bb2b8770fe9b765211eb16f

The exe will take 3 arguments, the project file, the output file and a bool to indicate whether or not to open after execution.

Example:

C:\TestProjects...>SourceCombiner.exe C:\ProjectFile.csproj C:\temp\output.cs true
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.