How can I access .net assembly Enums?

3 Ansichten (letzte 30 Tage)
Charles Chen
Charles Chen am 28 Sep. 2018
Beantwortet: David am 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.

Akzeptierte Antwort

Charles Chen
Charles Chen am 2 Okt. 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.

Weitere Antworten (2)

David
David am 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 am 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 Kommentar
Raha
Raha am 15 Mär. 2023
This was very helpful thanks fso much

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by