-
Notifications
You must be signed in to change notification settings - Fork 2
Importing and using .NET Types and Objects.
Maximilian Winter edited this page Apr 17, 2022
·
1 revision
You can register .Net types in the BiteProgram Type Registry and import these types into a module. For example, to get and use the static class System.Console
from C#, you first has to register it in a BiteProgram through the TypeRegisty RegisterType Function.
program.TypeRegistry.RegisterType (typeof(System.Console),"Console");
The first parameter is the C# Type, the second is an alias used in Bite to identifiy the class.
After this, you can use the registered class, in Bite code, through the NetLanguageInterface
like so:
// Get the static class System.Console
var Console = NetLanguageInterface("Console");
// Use the static class and call the method WriteLine.
Console.WriteLine("Hello World!");
The following code shows how to create an C# Object in Bite by calling his constructor and the use after it:
// Create an instance of TestClassCSharp, the first argument of NetLanguageInterface function is the class name,
// the second argument is a boolean that signals to the NetLanguageInterface to create an object.
// The third argument is a argument for the constructor and the fourth is its C# type.
// Constructor Arguments are passed to the NetLanguageInterface like this: constructorArgument, constructorArgumentCSharpType
var TestCSharp = NetLanguageInterface("TestClassCSharp", true, 42, "int");
TestCSharp.PrintVar();
PrintLine(TestCSharp.testfield.i);
TestCSharp.testfield.i = 58;
PrintLine(TestCSharp.testfield.i);
The corresponding C# Classes:
public class Foo
{
public int i = 5;
}
public class TestClassCSharp
{
public static int t = 45;
public double i = 5;
public Foo testfield { get; set; } = new Foo();
#region Public
public TestClassCSharp()
{
i = 0;
}
public TestClassCSharp( int n )
{
i = n;
}
public TestClassCSharp( int n, double x, int y, float z )
{
i = n * x * y * z;
}
public void PrintVar()
{
Console.WriteLine( i );
}
#endregion
}
Here are more examples of the use of the NetLanguageInterface