How can I access .net assembly Enums?

10 次查看(过去 30 天)
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.

采纳的回答

Charles Chen
Charles Chen 2018-10-2
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.

更多回答(2 个)

David
David 2023-9-13
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 2019-1-11
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.

类别

Help CenterFile Exchange 中查找有关 Getting Started with Microsoft .NET 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by