To access a value of a variable, XSharper transformations are used:
<!-- Print Hello world! --> <set name="x" value="Hello" /> <print>${x} world!</print>
Multi-expression is a piece of text included between the special characters ${ } (${{...}} or [[ ... ]] may be used instead, as described below ).
The word "multi-" means that several expressions may be concatenated with | and the first set value is chosen.
<!-- Prints TestB because variable a is not defined and b is defined --> <set name="b" value="TestB" /> <set name="c" value="TestC" /> <print>${a|b|c}</print>
There are different types of expressions, and the expression type is determined by the first character of an expression as explained below.
If first character of an expression is = , it is interpreted using rules close to those of C#, although much more forgiving in conversions between types.
For example (int)"20d" will throw an exception in normal C#, yet will produce a double value of 20.0 in XSharper calculated expression.
<!-- EXP1.XSH will print Type of 'num' is System.String Type of 'text' is System.String Expression value=52 ArrayLength=3 'aa' is larger than 'bb'=False C:\Windows has 226 files --> <set num="20" /> <set text="Hello" /> <print>Type of 'num' is ${=$num.GetType().FullName}</print> <print>Type of 'text' is ${=$text.GetType().FullName}</print> <print>Expression value=${= 2*$num+($text.Length+1)*2}</print> <print>ArrayLength=${= new int[] { 1,2,3}.Length }</print> <print>'aa' is larger than 'bb'=${= 'aa' #GT# 'bb' }</print>
Some notable differences from C# syntax:
#OR# | || |
#AND# | && |
#BOR# | | |
#BAND# | & |
#BXOR# | ^ |
#EQ# | == |
#NEQ# | != |
#LT# | < |
#GT# | > |
#LE# | <= |
#GE# | >= |
#NOT# | ! |
#NEG# | ~ |
This allows shorter syntax like .Print('a,b,c') instead of Context.Print('a','b','c'). Or .RegistryGet("HKLM\SOFTWARE\Microsoft\.NETFramework\InstallRoot",null) instead of XS.Utils.RegistryGet("HKLM\SOFTWARE\Microsoft\.NETFramework\InstallRoot",null).
If an expression is a valid number, it is the value of the expression.
<!-- prints 2000 --> <print>${2e3}</print>
If an expression starts with ', or ", or `, the expression is treated as string (expecting a matching character at the end).
<!-- prints Michael Jackson --> <print>Michael ${'Jackson'}</print>
If the first character is %, environment variable is used:
<!-- EXP2.XSH will print command interpreter=C:\Windows\system32\cmd.exe home drive=C: xsh_path=[NULL] --> <print tr='expand trim multiline'> command interpreter=${%COMSPEC%} home drive=${%HOMEDRIVE%} xsh_path=${%XSH_PATH%|'[NULL]'} </print>
By default, environment variables of the current process are accessed. To access variables of the current user or machine, user: and machine: prefixes are used instead. For example:
<print>System PATH=${%machine:PATH%}</print>
If all the previous checks fail, the value is treated as variable name. Empty variable names are also allowed:
<!-- Prints The desert is Tiramisu, as the variable desert is not set --> <set name="" value="Tiramisu" /> <print>The desert is ${desert|}</print>
Multiple expressions may be interpreted in sequence using
<eval> c.WriteLine(`Length of AAA `+'AAA'.length); c.WriteLine("Length of BB "+'BB'.length); c.Set("a",20); c.Print("A=${a}"); </eval>
The script below makes a screenshot and saves it to scrshot.png:
<reference name="System.Windows.Forms" addUsing="true" /> <eval> c.Set('v',SystemInformation.VirtualScreen); c.Set('bmp',new Drawing.Bitmap($v.Width,$v.Height)); c.Set('g',Drawing.Graphics.FromImage($bmp)); $g.CopyFromScreen( new Drawing.Point(0,0), Drawing.Point.Empty, $bmp.Size); $bmp.Save('scrshot.png'); </eval>