MWObjectArray
, a special subclass of MWArray
,
lets you create a MATLAB® array that references .NET objects.
Note:
For information about these data conversion classes, see the MATLAB
MWArray Class Library Reference, available in the |
You can create a MATLAB code wrapper around .NET objects
using MWObjectArray
. Use this technique to pass
objects by reference to MATLAB functions and return .NET objects.
The examples in this section present some common use cases.
To pass an object into a MATLAB Compiler SDK™ assembly:
Write the MATLAB function that references a .NET type:
function addItem(hDictionary, key, value) if ~isa(hDictionary,'System.Collections.Generic.IDictionary') error('foo:IncorrectType', ... 'expecting a System.Collections.Generic.Dictionary'); end hDictionary.Add(key, value); end
Create a .NET object to pass to the MATLAB function:
Dictionary char2Ascii= new Dictionary(); char2Ascii.Add("A", 65); char2Ascii.Add("B", 66);
Create an instance of MWObjectArray
to
wrap the .NET object:
MWObjectArray MWchar2Ascii= new MWObjectArray(char2Ascii);
Pass the wrappered object to the MATLAB function:
myComp.addItem(MWchar2Ascii,'C', 67);
You can also use MWObjectArray
to clone an
object inside a MATLAB Compiler SDK .NET Assembly. Continuing with
the example in Passing a .NET Object into a MATLAB Compiler SDK .NET Assembly, perform the following
steps:
Write the MATLAB function that references a .NET type:
function result= add(hMyDouble, value) if ~isa(hMyDouble,'MyDoubleComp.MyDouble') error('foo:IncorrectType', 'expecting a MyDoubleComp.MyDouble'); end hMyDoubleClone= hMyDouble.Clone(); result= hMyDoubleClone.Add(value); end
Create the object:
MyDouble myDouble= new MyDouble(75);
Create an instance of MWObjectArray
to
wrap the .NET object:
MWObjectArray MWdouble= new MWObjectArray(myDouble); origRef = new MWObjectArray(hash);
Pass the wrappered object to the MATLAB function and retrieve the returned cloned object:
MWObjectArray result= (MWObjectArray)myComp.add(MWdouble, 25);
Unwrap the .NET object and print the result:
MyDouble doubleClone= (MyDouble)result.Object; Console.WriteLine(myDouble.ToDouble()); Console.WriteLine(doubleClone.ToDouble());
When calling the Clone
method on MWObjectArray
,
the following rules apply for the wrapped object.
If the wrapped object is a ValueType
,
it is deep-copied.
If an object is not a ValueType
and
implements ICloneable
, the Clone
method
for the object is called.
The MemberwiseClone
method is called
on the wrapped object.
Calling Clone on MWObjectArray
For a full example of how to use MWObjectArray
to
create a reference to a .NET object and pass it to a component, see
the Optimization (C#) (C#)
and the Optimization (Visual Basic).
Every ASP .NET web application deployed to IIS is launched in
a separate AppDomain
.
The MATLAB .NET interface must support the .NET type wrapped
by MWObjectArray
. If the MWObjectArray
is
created in the default AppDomain
, the wrapped type
has no other restrictions.
If the MWObjectArray
is not created in the
default AppDomain
, the wrapped .NET type must be
serializable. This limitation is imposed by the fact that the object
needs to be marshaled from the non-default AppDomain
to
the default AppDomain
in order for MATLAB to
access it.