Native Types
Updated by Scott Waldron
Dino can access nearly all native types (.NET classes), along with their public methods (instance or static), public properties, public events, public fields, and public indexers. Dino can also handle special types like Enums using familiar syntax which is very close to C#.
Importing a native type is simple. Assuming the sandbox allows the type to be imported, you don’t even need to add a specific DLL or assembly – Dino will search the AppDomain for the class in the namespace that you specify.
Assumes that the System (mscorlib) assembly is available
from System import string;
from System.IO import File, Directory;
Assumes that the System.Core assembly is available
from System.Linq import Enumerable;
Once you have one or more native types imported, you can construct new instances, call static methods, connect to events, etc.
from System import File, FileInfo;
// construct an object – note that we don't use 'new'!
var finfo = FileInfo('C:\\Docs\\resume.docx');
with stream = finfo.OpenRead() {
// do something with the file stream
}
// call static method
File.Delete('C:\\temp\\tempfile.txt');
Interfaces
As a rule, you usually don’t need to worry about interfaces in Dino. After all, it’s a dynamic language – if the object has the method and your Dino code matches the signature, it will call the method. There is one notable exception to this rule…
The native language has the concept of explicit interface implementation. This causes a problem for dynamic languages like Dino, because explicitly implemented methods and properties simply do not exist on the object instances on which they are defined. Consider the following interface, and a class that implements that interface.
public interface IFoo
{
int Add(int x);
string Value
{
get;
set;
}
}
public class MyFoo : IFoo
{
private string m_Value;
// explicitly implemented method
int IFoo.Add(int x)
{
return x + 10;
}
// explicitly implemented property
string IFoo.Value
{
get
{
return m_Value;
}
set
{
m_Value = value;
}
}
}
In our example, the class MyFoo implements the IFoo interface explicitly. Any attempt to resolve the methods or properties of IFoo using a MyFoo instance will fail.
Dino handles this situation by treating interfaces as static method calls. Here is how we could work with an instance of MyFoo from Dino:
from MyStuff import MyFoo, IFoo;
// create a MyFoo
var fooInst = MyFoo();
// var a = fooInst.Add(10); // FAILS 'method not found'
//
// call an explicit interface method instead
var a = IFoo.Add(fooInst, 10); // 20
// set and get explicit interface property
IFoo.set_Value(fooInst, 'test');
var v = IFoo.get_Value(fooInst); // 'test'
Notice that when we call an explicit interface implementation, we use the type name of the interface, followed by a dot, followed by the method name. For arguments, we must pass the instance of the object as the first parameter, as an extra argument. Additionally, you may have noticed that odd syntax for accessing the explicit property: because there would be no syntax that would work to get and set property values (consider: IFoo.Value ='abc'; // which instance of IFoo??
), we must call the special name methods that underlie the property. So, the get accessor for Value in our example becomes a method called get_Value, and the set accessor becomes set_Value.
If this seems like a lot to remember – it is! If you don’t know if the object has implemented the interface(s) explicitly, just try to access the methods and properties in the normal manner. If it fails, then you can fall back to using this odd syntax.