How can I access .net assembly Enums?

조회 수: 17 (최근 30일)
Charles Chen
Charles Chen 2018년 9월 28일
답변: David 2023년 9월 13일
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.
  댓글 수: 1
Raha
Raha 2023년 3월 15일
This was very helpful thanks fso much

댓글을 달려면 로그인하십시오.

카테고리

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