Reusing Enum.HasFlag for use in Mono 2.6
I am trying to convert Jint for use in Mono 2.6. Unfortunately Mono 2.6 doesn't have Enum.HasFlag and whatever they use in Jint. I should also add that I am fairly new to C #.
According to the MSDN page ( http://msdn.microsoft.com/en-us/library/system.enum.hasflag(v = vs. 110) .aspx ) the implementation should be
thisInstance And flag = flag
but that doesn't seem to make much sense. If these are all bitwise operations, shouldn't there be more of that?
thisInstance & flag == flag
So, the line I'm trying to change is
Writable = !fieldInfo.Attributes.HasFlag(FieldAttributes.InitOnly);
I'm stuck in
var thisInstance = fieldInfo.Attributes;
var thisFlag = FieldAttributes.InitOnly;
var hasFlag1 = thisInstance & thisFlag == thisFlag;
var hasFlag2 = thisInstance And thisFlag = thisFlag;
Writable1 = !hasFlag1;
Writable2 = !hasFlag2;
and understandably the compiler doesn't like any of them. For hasFlag1 I get
Operator '&' cannot be applied to operands of type 'System.Reflection.FieldAttributes' and 'bool'
And for hasFlag2:
Unexpected symbol 'And'
Just want to know if anyone knows how this should be done.
Thank!
source to share
It seems that based on a compiler error, == takes precedence over &. So your string is evaluated like this: var hasFlag1 = thisInstance and (thisFlag == thisFlag);
What you want is this:
var hasFlag1 = (thisInstance & thisFlag) == thisFlag;
So if you add parentheses the compiler error should go away.
Most likely the And
VB equivalent&
source to share