IKernelLinkPutReference(Object, Type) Method

Sends the object cast as a parent type or interface.

Definition

Namespace: Wolfram.NETLink
Assembly: Wolfram.NETLink (in Wolfram.NETLink.dll) Version: 2.0.0.0
C#
void PutReference(
	Object obj,
	Type t
)

Parameters

obj  Object
The object to send by reference.
t  Type
The type the object will be seen as in the Wolfram Language.

Remarks

Use this form of PutReference if you want to "upcast" the object so that it is seen in the Wolfram Language as an instance of a parent class or interface. You would want to do this if you needed to call from Wolfram Language an inherited version of a method that was hidden by a "new" declaration lower in the inheritance hierarchy. For example, consider the following classes:
C#
public class Parent {
    public virtual string Foo() { return "parent foo"; }
}

public class Child {
    public new string Foo() { return "child foo"; }
}
In C# code, if you had an object of type Child and you wanted to call the Parent version of Foo(), you would upcast the object like this:
C#
Child childObject = new Child();
string result = ((Parent) childObject).Foo();
This PutReference() method is the equivalent for Wolfram Language programmers. You would send the object to the Wolfram session typed as Parent, so that when you called the Foo() method you would get the version from the Parent class.

An alternative is to use the other signature of PutReference() to send the object to the Wolfram Language as its normal type and then call the Wolfram Language function CastNETObject[] to upcast it to a parent type.

For more examples of when you would want to use this method, see the .NET/Link User Guide for its discussion of the CastNETObject Wolfram Language function.

This method is virtually never used as a "downcast". Downcasting is generally irrelevant in .NET/Link because objects are normally sent to the Wolfram session as their true runtime type (there is no lower-down type to cast to). It is only useful to upcast to a parent type or interface. The one exception to this is if you have an object that you have previously upcast to a parent type and you want to downcast it back to its true runtime type.

Exceptions

MathLinkExceptionOn any MathLink error.
InvalidCastExceptionIf the object cannot be cast to the given type.

See Also