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();
I can call myFunction through the IClass interface however:
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
Roberto Olmi
Roberto Olmi on 26 Aug 2020
I add that if you need to access interfaces properties you have to add get_ (or set_) before the property name. For example (consider your interface having a property named "PropertyName"):
propValue = t.InvokeMember('get_PropertyName',System.Reflection.BindingFlags.InvokeMethod,[],obj,[])

Sign in to comment.

More Answers (0)


No tags entered yet.




Community Treasure Hunt

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

Start Hunting!