Can I call hidden methods from a .NET Object through the Interface that it implements in MATLAB 7.13 (R2011b)?

Suppose I have a .NET Assembly compiled from the following C# code:
namespace ClassLibrary1
{
public interface IClass
{
String myFunction(String s);
}
public class Class1 : IClass
{
String IClass.myFunction(String s)
{
return String.Format("Hello {0}", s);
}
}
}
As you can see I have used Implementation Hiding for myFunction. Or in other words in C# I CANNOT call myFunction as follows:
ClassLibrary1.Class1 obj = new ClassLibrary1.Class1();
obj.myFunction("World");
I can call myFunction through the IClass interface however:
((ClassLibrary1.IClass)obj).myFunction("World");
Can I also do this in MATLAB?

 Accepted Answer

Because MATLAB is not a strongly typed language it is not possible to first cast the object instance to the interface. Using reflection, it is still possible to call the method through the interface though; the following code shows this:
%%Load Assembly
A = NET.addAssembly([pwd '\ClassLibrary1.dll']);
% Get Type information for the IClass interface
t = A.AssemblyHandle.GetType('ClassLibrary1.IClass');
%%Create an object instance
obj = ClassLibrary1.Class1;
%%Call the method (through reflection)
% First define an Object[] for the inputs
inputs = NET.createArray('System.Object',1);
% Define the actual inputs
inputs(1) = System.String('World');
% Call the method
t.InvokeMember('myFunction',System.Reflection.BindingFlags.InvokeMethod,[],obj,inputs)

1 Comment

This produces the following warning for my class: The interface does not support late bound calls since it does not derive from IDispatch.
Any other workarounds?

Sign in to comment.

More Answers (0)

Products

Release

R2011b

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!