CS-Script 3.20.0


Passing well-known type between script and host

Passing (or accessing) "well-known" type between the script and the host is a particular case of the "type sharing"  pattern. Script and host applications can pass run-time objects in both directions.

Because the script and the host do not share any local assembly the only way for them to understand each other is to only pass the objects implemented in GAC assemblies. These types can be strings, datasets, images etc.

Example

Calling static methods

Let's assume we have the script translator.cs, which contains the implementation of translating from English to Spanish and vice versa. The translator class has two methods ToSpanish and ToEnglish, which are the word conversion to upper and lower case (to simulate the translation).

using System;

class EngSpanTranslator
{
    static  string ToSpanish(string word)
    {
        return word.ToLower();   
    }

    static  string ToEnglish(string word)
    {
        return word.ToUpper();
    }  
}


The host application can pass and receive strings to and from the script because string is a type which is implemented in the System.dll assembly and this assembly belongs to the GAC.

The following is an example of how the host application can call script's static methods:

AsmHelper script = new AsmHelper(CSScript.Load("scripts/translator.cs", null, true));

string engWord  = "HELLO";
string spanWord = (string) script.Invoke("EngSpanTranslator.ToSpanish", engWord); 

 

In the code above the value of the spanWord variable after the script execution would be "hello".

The completely functional example of calling the script static methods is described in the tutorial Text Processor.

 

Calling instance methods

Let's assume now that the translator class have only the instance methods.

using System;

class EngSpanTranslator
{
    string ToSpanish(string word)
    {
       return word.ToLower();   
    }


    string ToSpanish(string word)
    {
        return word.ToUpper();
    }  


The starting point of any script hosting is compilation of the script into assembly (CSScript.Load). After that it is possible to use Reflection for this assembly to create the type instance and call it's methods. However, it can be quite painful to maintain such code. Instead you may inherit your class from the well-known suitable interface. In our example it would be IEngSpanTranslator implemented in translators.dll, which is registered with GAC. In fact it can be any interface from FCL (IComparer, IComponent, IPropertyBrowser etc.).

namespace Translators
{
    interface IEngSpanTranslator
    {
        string ToSpanish(string word);
        string 
ToEnglish(string word);
    }
}

 
The script in this case should be modified like this: 

using System;
using Translators;

class EngSpanTranslator : IEngSpanTranslator
.....

This is how the host application can call the script's instance methods:

AsmHelper script = new AsmHelper(CSScript.Load("scripts/translator.cs", null, true));
IEngSpanTranslator translator =
                     (IEngSpanTranslator) script.CreateObject("EngSpanTranslator");

string engWord  = "HELLO";
string spanWord = translator.ToSpanish(engWord);

As you can see only the instantiation of the type is something unusual. Calling the type methods is just as for any C# code.

Using interfaces can be very beneficial because it enforces proper separation between a service provider (script) and a service consumer (host).

A completely functional example of calling the script instance methods is described in the tutorial Text Processor.

See Also

Reference  | Tutorial (Text Processor)