Tutorial 2. Creating custom function

SSharp S# API

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

It is possible to extend S# runtime with a custom function. For this purpose two requirements should be met:

  • A class implementing IInvokable interface should be created;
  • An instance of this class should be associated with a variable name, via script context.

Here is an example of custom function:

 

     RuntimeHost.Initialize();        

    Script script = Script.Compile(@"

       return Test();

     ");

     script.Context.SetItem("Test", new TestFunction());

 

    object result = script.Execute();

 

    Assert.AreEqual(10, result);

 

where:

 

public class TestFunction : IInvokable

 {

   #region IInvokable Members

 

  public bool CanInvoke()

   {

    return true;

   }

 

  public object Invoke(IScriptContext context, object[] args)

   {

    return 10;

   }

 

   #endregion

 }