How can I access .net assembly Enums?

4 vues (au cours des 30 derniers jours)
Charles Chen
Charles Chen le 28 Sep 2018
Réponse apportée : David le 13 Sep 2023
I have a Net assembly with Enums like below: FPGA3_Library.InitiateBoard+FPGABoardStates
In Matlab R2016a, I can directly access it by using enumtest = FPGA3_Library.FPGABoardStates;
But in Matlab R2018b, when I use the code, I get an error Undefined variable "FPGA3_Library" or class "FPGA3_Library.FPGABoardStates".
Could anyone give me some clue on this?
Thanks.

Réponse acceptée

Charles Chen
Charles Chen le 2 Oct 2018
I found a working solution from other source.
To access nested Enum, use below command: enumtest = FPL.AssemblyHandle.GetType('FPGA3_Library.InitiateBoard+FPGABoardStates');
Then use enumtest.GetEnumValues to get all enum values.

Plus de réponses (2)

David
David le 13 Sep 2023
And perhaps add a slight bit of clarity, you definitely do need the round brackets and single quotes.
So continuing the original post example: if the valid enum FPGABoardStates are "On" or "Off" you can assign a matlab variable like so:
stateOn = FPGA3_Library.('InitiateBoard+FPGABoardStates').On;
stateOff = FPGA3_Library.('InitiateBoard+FPGABoardStates').Off;
then
whos stateOn
would let you confirm the type of this variable.
Then you could pass this variable (as an argument of the specific enum type) to a method/function of the class as appropriate.

Telencephalon
Telencephalon le 11 Jan 2019
Just ran into this issue, too, and the accepted answer helped me solve it. To elaborate a bit more: When an assembly is added like this:
myAssemblyObject = NET.addAssembly('C:\path\to\myAssembly.dll');
then a handle to a nested enum (i.e., an enum that is a member of a class) can be accessed by using
myEnumHandle = myAssemblyObject.AssemblyHandle.GetType('My.Namespace.MyClass+MyNestedEnum');
Now, a list of the values of the enum can be accessed by
myEnumHandle.GetEnumValues()
and an individual value at index ix by
myEnumHandle.GetEnumValues().Get(ix)
All the handles (to the assembly, the enum, and the enum values) offer a wealth of additional properties and methods that can be explored by using tab completion in the Matlab command window.
  1 commentaire
Raha
Raha le 15 Mar 2023
This was very helpful thanks fso much

Connectez-vous pour commenter.

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by