CS-Script 3.27.0


Script Post-Processing


Sometimes it is desirable to do certain manipulations with the compiled script assembly just after it has been compiled but before it is executed. CS-Script allows such manipulations by allowing the user to specifying the external Post-Processor assembly, which is capable of conducting such manipulations. Such assembly must have very specific interface and it can be specified in the Configuration Console.

The interface of Post-Processor assembly is quite simple. The following code snippet demonstrates the an example of simple Post-Processor implementation:


public class CSSPostProcessor

{

    /// <summary>

    /// Processes the specified script assembly before its execution.

    /// </summary>

    /// <param name="assemblyPath">

    /// The compiled script assembly to be processed.</param>

    /// <param name="refAssemblies">

    /// The assemblies referenced by the script.</param>

    /// <param name="probingDirs">The assembly probing directories.</param>

    public static void Process(string assemblyPath, string[] refAssemblies,

                                                    string[] probingDirs)

    {

        //process (modify) the assembly at the assemblyPath    

    }

}


The Process method is called by the CS-Script engine when the script being executed is compiled into assembly and is about to be loaded and invoked. The name of the assembly file is irrelevant
 
The possible applications of the script assembly Post-Processing is obfuscation or aspect injection. Thus starting from v2.6 CS-Script comes Lib/CSSPostSharp.dll assembly for injecting PostSharp aspects. The example can be found in Samples\PostSharp directory (read readme.txt file for the details).

The following is the example of injecting PostSharp aspect for logging all method invocations of the types defined in System.Threading namespace

//css_ref PostSharp.Public.dll;
//css_ref PostSharp.Laos.dll;
using System;
using PostSharp.Laos;
using System.Threading;
 
[assembly: PostSharp.Laos.Test.MyOnMethodInvocationAspect(
    AttributeTargetAssemblies = "mscorlib",
    AttributeTargetTypes = "System.Threading.*")]
 
namespace PostSharp.Laos.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main is executed");
            Thread.Sleep(1000);
            Console.ReadLine();
        }
    }

    [Serializable]
    public class MyOnMethodInvocationAspect : OnMethodInvocationAspect
    {
        public override void OnInvocation(MethodInvocationEventArgs context)
        {
            Console.WriteLine("Calling {0}", context.Delegate.Method);
            context.Proceed();
        }
    }
}