Skip to content

Imports

Espen Skaufel edited this page Oct 13, 2018 · 2 revisions

Importing properties and static functions

By importing a Type like System.Math you get access to all of the properties and static functions.

Public property

var instance = new ComputeInstance();
instance.Builder.Imports.AddType(typeof(Math));

const string key = "math_property";

instance.AddExpression(key, "E"); // E is Math.E property
var result = instance.GetResult(key);
Assert.AreEqual(Math.E, result); // e = 2.71828182845905

Static function call

var instance = new ComputeInstance();
instance.Builder.Imports.AddType(typeof(Math));

const string key = math_sqrt;

instance.AddExpression(key, "Sqrt(16)"); //Sqrt is Math.Sqrt(double d)
var result = instance.GetResult(key);
Assert.AreEqual(4, result);

Namespacing type imports

In some cases it can be convenient to organize imports into groups/namespaces. This is done by adding the namespace string as a second argument in the AddType or AddMethod call

instance.Builder.Imports.AddType(typeof(Math), "Group1");

const string key = "math_function";

instance.AddExpression(key, "Group1.Sqrt(16)");
var result = _instance.GetResult(key);

Assert.AreEqual(4.0, result);

Method imports

You can import one specific method using the add

var instance = new ComputeInstance();
instance.Builder.Imports.AddMethod("Sqrt", typeof(Math), "Test");

instance.AddExpression("key", "Test.Sqrt(16)");
var result = instance.GetResult("key");

Assert.AreEqual(4.0, result);