How can I access .net assembly Enums?

17 visualizzazioni (ultimi 30 giorni)
Charles Chen
Charles Chen il 28 Set 2018
Risposto: David il 13 Set 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.

Risposta accettata

Charles Chen
Charles Chen il 2 Ott 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.

Più risposte (2)

David
David il 13 Set 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 il 11 Gen 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.

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by