tags:

views:

228

answers:

2

I am having all sort of trouble with [FlagsAttribute] enums in Matlab. It appears there is no way to pass a combination of values as a parameter to .NET. For example, BindingFlags.Public|BindingFlags.Instance, once you combine these together in Matlab they become of an internal type and cannot be cast back to BindingFlags.

Things like

import System.*;
import System.Reflection.*;

flags = BindingFlags.Public+BindingFlags.Instance;
enum  = Enum.ToObject(Type.GetType('System.Reflection.BindingFlags'), int32(flags));

or

enum = Enum.Parse(Type.GetType('System.Reflection.BindingFlags'), 'Public, Instance');

simply don't work as it reads:

??? One or more output arguments not assigned during call to "System.Enum.ToObject" (or "System.Enum.Parse").

On the other hand

enum = Enum.Parse(Type.GetType('System.Reflection.BindingFlags'), 'Public');

works just fine and returns <1x1 System.Reflection.BindingFlags> set to 'Public'.

That goes, needless to say, for all [FlagsAttribute] enums.

Am I missing something here? Writing a C# reflection enum wrapper for Matlab is not a big deal but that would slow things down enormously. Any workaround would be deeply, deeply appreciated.

A: 
Richie Cotton
Unfortunately, this is not how I described the problem. All your code is in C#. All my code is in Matlab (that is why naming a variable "enum" is completely legit). So, please, read my question carefully. There is no water in it. Appreciate your input anyway.
jbarr
Oh, yes, and thank you for properly formatting my question in the first place. I hope once you take a second look you are able to help me out.
jbarr
Oh well. Second attempt. "EnumToObject" function creates and returns cell array of enums but fails to accomplish the task in question: setting an enum object to a value "20" or whatever else combination of flags (as opposed to single flag, like "Public") from Matlab.
jbarr
"EnumParse" is meaningless because we cannot get the the string "Public, Instance" from anywhere but make it up ourselves! Matlab just won't return the [FlagsAttribute] enums set to a combination of values. It gives us <0x0 System.Reflection.BindingFlags> instead.And there is no need to parse our own string...I think it's about time we drop this question until the next release comes out.Now, I want to thank you for your time and efforts spent. Appreciate it.
jbarr
"Until the next release comes out" was sadly too optimistic. I'm working with MATLAB 2010b, and the problem is still there.
Andrew Shepherd
A: 

(This isn't an answer, just more information)

At the time of writing, the latest version of MATLAB(2010b) does not provide a .NET interface that lets you work with flag enumerations.

One of the fundamental pieces of .NET that MATLAB does not support is casting. This means a line of code like this:

myControl.Anchor = (AnchorStyles)(AnchorStyles.Top | AnchorStyles.Bottom)

Cannot be written in MATLAB.

The .NET method System.Enum.Parse would be ideal for solving the problem of creating combined flags enumerations in MATLAB. But, when invoked from MATLAB, System.Enum.Parse is broken.

Try this:

>> NET.addAssembly('System.Windows.Forms');
>> anchorStylesTypeName = 'System.Windows.Forms.AnchorStyles, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089';
>> anchorType = System.Type.GetType(anchorStylesTypeName);
>> enum = System.Enum.Parse(anchorType, '1')

enum = 

   Top
>> 
>> % That looked good!
>> enum = System.Enum.Parse(anchorType, '2')

enum = 

    Bottom
>>
>> % OK, now we combine them...
>> enum = System.Enum.Parse(anchorType, '3')
??? One or more output arguments not assigned during call to
"_class_dot_paren".

The same function works fine when run in C#: Passing "3" to System.Enum.Parse returns "Top, Bottom".

Andrew Shepherd