Properties and methods are not displayed for a .net object
Asked Answered
C

1

7

I have .NET assembly file and I need for it to work in MATLAB. (The library was created in C# and I have the corresponding source code)

Following the documentation, I found out that the following command will load up the assembly in MATLAB and make its classes available for use "in MATLAB". But it does not appear to be working. I used this to load up the file:

  color = NET.addAssembly('c:\path\to\file\EvolutionMapsClassLib.dll');

It load up fine and I see a 1x1 .NET assembly object in my workspace.. When I type color I get the following result:

  color = 

    NET.Assembly handle
    Package: NET

  Properties for class NET.Assembly:

      AssemblyHandle
      Classes
      Structures
      Enums
      GenericTypes
      Interfaces
      Delegates

So apparently it has loaded up properly, furthermore typing color.Classes gives the following:

  >> color.Classes

  ans = 

      'EvolutionMaps.EvolutionMap'
      'EvolutionMaps.EvolutionMap+EstimationResults'
      'EvolutionMaps.PrincipalDirectionEvolutionMap'
      'EvolutionMaps.CharacterDimensionsEstemator'
      'EvolutionMaps.MapBlob'
      'EvolutionMaps.MapsMetric'
      'EvolutionMaps.MapsMetric+MapMinimalComparable'
      'EvolutionMaps.MapsL2Distance'
      'EvolutionMaps.DiagonalEvolutionMap'
      'EvolutionMaps.EvolutionMapGenerator'
      'EvolutionMaps.HeightEvolutionMap'
      'EvolutionMaps.FullnessEvolutionMap'
      'EvolutionMaps.YvalEvolutionMap'
      'EvolutionMaps.ImageExtractor'
      'EvolutionMaps.HorisontalProjectionDistance'
      'EvolutionMaps.StrokeWidthEvolutionMap'
      'EvolutionMaps.ConnectedComponentsFinder'
      'EvolutionMaps.ColorMap'
      'EvolutionMaps.ColorMap+GrayColorMap'
      'EvolutionMaps.ColorMap+JetColorMap'
      'EvolutionMaps.TransitionAvgEvolutionMap'
      'EvolutionMaps.PrincipalProjectionEvolutionMap'
      'EvolutionMaps.ConnectedComponent'
      'EvolutionMaps.WidthEvolutionMap'

That appears to be working well, but according to the online help, in order to interact with these classes I need to know the methods and properties.

this is where I am having problems, as neither properties nor methods seem to work. I tried every variation to get the properties or the methods list but I keep getting this error:

  >> properties color.EvolutionMaps.ColorMap

  No properties for class color.EvolutionMaps.ColorMap or no class color.EvolutionMaps.ColorMap.

  >> properties color.Classes.EvolutionMaps.ColorMap

  No properties for class color.Classes.EvolutionMaps.ColorMap or no class color.Classes.EvolutionMaps.ColorMap.

  >> properties Classes.EvolutionMaps.ColorMap

  No properties for class Classes.EvolutionMaps.ColorMap or no class Classes.EvolutionMaps.ColorMap.

Same is the case with methods, I keep getting this error:

  >> methods color

  No methods for class color or no class color.

Where as when I open the source code for this assembly it shows all the methods and properties as can be seen from this screenshot.

So how can I make the .NET assembly work without it displaying properties or methods?

Thank you

Cullin answered 21/2, 2014 at 17:39 Comment(2)
In the C# code, are the properties static or not? Maybe they need to be public; are they?Hereat
@JeppeStigNielsen Within the class, all the variables are private but the functions are public. Would it help if you saw the code? I can not upload it here as its not mine but I can email it to you if you would like to have a look. Thank youCullin
I
5

The NET.addAssembly function loads the .NET assembly and makes it available in MATLAB. The variable returned is actually a "meta object" containing information about the classes, enums, structs, etc.. contained in the assembly.

If you want to work with whatever classes are exposed by the library, you have to instantiate objects as usual (or call static functions directly if available). The documentation explains this very well.

As an example, take the following C# class:

MyClass.cs

using System;

namespace ClassLibraryTest
{
    public class MyClass
    {
        public static double add(double x, double y)
        {
            return x + y;
        }

        public double negate(double x)
        {
            return -x;
        }
    }
}

First we compile it into an .NET assembly, the we use it in MATLAB:

> csc.exe /target:library /out:ClassLibraryTest.dll MyClass.cs

MATLAB

% load my assembly
info = NET.addAssembly( fullfile(pwd,'ClassLibraryTest.dll') );

% call static method
result = ClassLibraryTest.MyClass.add(1,2)

% instantiate instance of class and call member function
c = ClassLibraryTest.MyClass();
result = c.negate(1)

You can now inspect the properties and methods of the class as usual:

>> properties(c)
>> methods(c)
>> methodsview ClassLibraryTest.MyClass
Intracranial answered 24/2, 2014 at 13:20 Comment(2)
Thank you for your reply, Amro, I was hoping you would reply here. I tried to follow your directions and tried to instantiate an instance of a class with c = EvolutionMaps.ConnectedComponent(); but I get the error No constructor 'EvolutionMaps.ConnectedComponent' with matching signature found. Would it help if you viewed the code? I can email it to you. Thank you for your input on my questionCullin
Issue a methods EvolutionMaps.ConnectedComponent -full to see the signatures of the constructor. The constructors are those methods named as the class.Selfconscious

© 2022 - 2024 — McMap. All rights reserved.