Writing Xbasic wrapper classes for .NET classes


Xbasic has a simple method for defining native wrapper classes for .NET classes that can help you avoid writing registration code every time you use a class. Simply create a class in your project and make the body of your class the desired registration code. Then save the class file as <Namespace>.<Class> and reference it in your code as <Namespace>::<Class>. Note that the class filename includes a dot, while the class reference includes two colons.

While you can use your own namespace and class names, it makes life a whole lot simpler for you and anyone else who uses your class definition if you match the .NET class hierarchy. Also, do not bother to create a class file for a preloaded class.

The following example should make the methodology clear.

Example: Big Integers 

Class file contents:

dim Sv as DotNet::Services
dim assy as DotNet::AssemblyReference
assy.filename = DotNetPath()+"System.Numerics.dll"
Sv.RegisterClass("Numerics", "BigInteger", "System.Numerics.BigInteger", assy)

Save as Numerics.BigInteger to match the first two parameters of the RegisterClass method call. Then try this interactive session:

dim bi as Numerics::BigInteger
a = new Numerics::BigInteger(987654)
? bi.Pow(a,5).ToString()
= "939775534806134151589356093024"