CS-Script 3.27.0


Using Resources

In the majority of the runtime scripting scenarios there is no need to use resources beacuse of the dynamic nature of scripting a s such. However in some cases it cannot be avoided. Th CS-Script supports special directive for loading compiled resource file. Thus you can specify directly in script code what resource file should be loaded at execution time:

//css_resource <file>;

file - name of the resource file. The resource file is a compiler managed resource file (XML .resx or compiled .resources file)

The shorter alias directive //css_res can be used in place of the //css_resource.

File location

The resource file to be loaded must be from one of the following locations (the order indicates the file search priority):

Remember that you may need to escape some path characters that conflict with the //css_* delimiters. See Delimiters Escaping section.

The following code loads Scripting.Form1.resources resource file at execution time thus code in the Form1 implementation can access any resources from this file.

//css_res Scripting.Form1.resources;
using System;
using System.Windows.Forms;

class Test 

    static public void Main( string [] args)
    {
        Application.Run(new Form1());
    }
}

public class Form1 : System.Windows.Forms.Form
{
    ....
}

Of course a managed resource file represents some value of it's own but also it is a "must have" runtime component of some types for the managed applications. 

For example XAML applications (.NET3.0) retrieve compiled XAML file (BAML) from the application resources. This can be changed in the final release of the .NET3.0 but currently all standard XAML application created with WPF relay on use of managed resources. 

Another example is a WinForm application with ActiveX controls. Technically speaking you do not need to have managed resources in you application for this. However if your script application is created with the Visual Studio wizard it will always use managed resources to initialise ActiveX control at startup. It is possible to remove such initialization from the InitializeComponent() and run the code as a script but you may want to preserve InitializeComponent() from any modifications (as it can break a form designer). In this case you script will need to load resources. Thus if your Visual Studio project produces obj\Debug\MyApp.MyForm.resource file it needs to be included in your script.

//css_res MyApp.MyForm.resources;
using System;
using System.Windows.Forms;

namspace MyApp 
{
    class MyForm 
    { 
        .....


The interesting things is that it actually does not matter what resource file is used, ActiveX will be initialized successfully with any valid resource file. That is why for all scripts with ActiveX controls you can specify the same resource file. However this dummy resource file has to be appropriately named. :

//css_pre com(/ax, MSCAL.Calendar, AxInterop.MSACAL.dll);
//css_ref AxInterop.MSACAL.dll;
//css_res Scripting.Form1.resources;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Scripting
{
    public class Form1 : System.Windows.Forms.Form
    {
        ......

The first two lines produce a managed wrapper for MS Calendar ActiveX and instruct CS-Script to referenced produced wrapper assembly. The next line indicates that the CS-Script engine needs to load this resource file at execution time.

 
If it is required to generate not dummy but a real resources file you may generate it on fly from the XML resx file. This is what typically Visual Studio does for you when the project conteins .resx files.

//css_res Resources.resx;                              
 

using System;
using System.IO;
using System.Reflection;
using System.Resources;

class Script
{
    static public void Main()
    {
        var res = new ResourceManager("Resources", Assembly.GetExecutingAssembly());
                           
        Console.WriteLine("String1=\"{0}\"", res.GetObject
("String1"));
    }
}

See Also

Script Library | Using COM (Tutorial)