Search code examples
c#csharpcodeprovider

CSharpCodeProvider Compiling Order


Is there a way to compile files automatically in the right order. Seems like 'CompileAssemblyFromFile' do not care about dependencies before compiling. Way around ?

public bClass 
{
   public aClass FieldName; //Trows error not aClass type not found
}

Compile Order

1. bClass.css
2. aClass.cs

Solution

  • It works for me in either order:

    var pro = new CSharpCodeProvider();
    var assem = pro.CompileAssemblyFromFile(new CompilerParameters(), "path/to/bClass.cs", "path/to/aClass.cs");
    // or
    var assem = pro.CompileAssemblyFromFile(new CompilerParameters(), "path/to/aClass.cs", "path/to/bClass.cs");
    

    aClass.cs:

    public class aClass 
    {
    }
    

    bClass.cs:

    public class bClass 
    {
       public aClass FieldName;
    }