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

1 view (last 30 days)
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

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
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
Jim Hokanson
Jim Hokanson on 7 Jun 2014
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)

Tags

No tags entered yet.

Products


Release

R2011b

Community Treasure Hunt

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

Start Hunting!