Using C#

Code snippets

While XSharper has basic primitives like expressions, or if/else, or while loop, these are often not sufficient. Instead of reinventing the wheel, it is very easy to embed C# code into the script.

 

Everything inside <code> is a single class, so local variables defined in one code block are not accessible in another.

It's also possible to mix & match too by inserting actions into C# code. And even add methods to the class written in C#.

 

Some interesting notes:

  • there is a variable c, or Context, that can be used to access current script context, get/set variables etc.
  • for (int i=0;5>i;++i) construction, which is used to avoid the cumbersome &lt; instead of < sign. && is inconvenient for the same reason.

However, when inserting larger pieces of C# code, perhaps copy-pasted from somewhere, XML escaping becomes rather annoying. There are two possible solutions.

CDATA section:
 

or an XML processing instruction:
 

Which can be further shortened from <?code to <?_:

 

Headers

As everything inside <code> is inserted by XSharper into a class, it's not possible to define complete classes to be shared with different <code> actions, or define namespaces.

To insert code outside of any class or execution sequence, there is a <header> action, which may be also inserted as <?header or <?h processing instruction.

 

References

To add a reference to another assembly, there is a <reference> action. Assemblies may be referenced by name (using name attribute) or by location (using from attribute)

 

or a bit shorter: