In the majority of the runtime scripting scenarios there is no need to use resources. 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:
file - name of the resource file. The resource file is a compiler managed resource file (etc. .resource compiled from .resx)
The shorter alias directive //css_res can be used in place of the //css_resource.
The resource file to be loaded must be from one of the following locations (the order indicates the file search priority):
the same directory where the script is
Default Script Library directory Script Library (%CSSCRIPT_DIR%\Lib)
Custom Script Library directory(s) (specified in the configuration console SearchDirs)
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 { .... } |
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 it has to
be appropriately named. This can be automated by running the res.cs
script as a pre-execution script. It is capable of producing
appropriate 'dummy' .resource file. The code below is a fragment of the
calendar.cs script which uses the Microsoft Calendar ActiveX control (full listing of the calendar.cs can be found here):
| //css_pre com(/ax, MSCAL.Calendar, AxInterop.MSACAL.dll); //css_ref AxInterop.MSACAL.dll; //css_pre res(dummy, Scripting.Form1.resources); //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 two lines produce a managed resource file Scripting.Form1.resources and indicate that the CS-Script engine needs to load this resource file at execution time.
The calendar.cs script can be run as a script or open in Visual Studio and manipulated through the FormDesigner without any code changes. All this because resources are prepared and associated with the script through "//css_res" and "//css_pre res" directives.
Note that calendar.cs sample can work only if MS Office is installed.
| //css_pre res(%CSSCRIPT_DIR%\csws.exe, script.resources); //css_res script.resources; using System; using System.IO; using System.Windows.Forms; using System.Reflection; using System.Resources; class Script { static public void Main(string[] args) { var data = new ResourceManager("script", Assembly.GetExecutingAssembly()) .GetObject("csws.exe"); File.WriteAllBytes("csws.exe", (byte[])data); } } |
Script Library | Using COM (Tutorial)