Monday, April 11, 2011

.Net Scripting Engine (C#) - Is there some sort of "header file" or "dll references file" for .Net languages?

I'm making a small scripting engine in C# and I was wondering, is there some sort of #compiler directives or some sort of other header-like file I can compile with my script files to define what Managed Dlls my script files are referencing?

public static CompilerResults LoadScript(
    string SourceCodeFile, string[] References)
{
    var Parameters = new CompilerParameters()
    {
     GenerateExecutable = false,
     GenerateInMemory = true,
     IncludeDebugInformation = false,
    };

    Parameters.ReferencedAssemblies.AddRange(References);

    return CSharpCodeProvider.CreateProvider(
     CSharpCodeProvider.GetLanguageFromExtension(
      Path.GetExtension(SourceCodeFile)))
       .CompileAssemblyFromFile(Parameters, SourceCodeFile);
}

I would love to be able to eliminate this line of code:

Parameters.ReferencedAssemblies.AddRange(References);
and put that information in my scripts instead. The problem is I do not necessarily know ahead of time what .dlls the scripts are going to reference. My current solution right now is to parse a custom "script.cs.refs" file that looks something like this:
"System.dll","System.Data.dll","SomeOther.dll","Ect.dll"
Any help on finding out where visual studio stores your references to dlls and if it has to be defined in a separate file, if there is a standardized format I should be using that I can load into the compiler that tells it this information, would be greatly appreciated!

From stackoverflow
  • You can't specify assembly references within C# source code, no.

    Visual Studio will pass the references in your project to the compiler when it builds the project.

    Note that when using the command line, there's the notion of a response file containing a bunch of options. This can be specified on the command line, or there's a default csc.resp file in the .NET framework directory which has default options including references, but I doubt that that's used from CSharpCodeProvider.

    You could potentially have your own "special comment" format which lists the references - parse the script that you've been given to grab those references, and then pass those in.

    Fox : That's what I was afraid of, I suppose I could define that information in a comment in at the top of the script files for now...
  • I don't think there is a standard solution to this, except that you could take the ASP.NET like approach of having the references to the assemblies defined in the source file.

    You would then need to

    1. Parse the source code file to find the assembly references
    2. Pass them to the CompilerParameters
    3. Make sure the lines defining the references in the source code do not generate compile errors.

    To achieve 3., you could have the references defined in comments. e.g.

    //@ Assembly name="SomeOther.dll"
    

    You could then regex the sourcode file for these //@ lines and parse out the assembly name.

0 comments:

Post a Comment