简体   繁体   English

重新实现Enum.HasFlag以在Mono 2.6中使用

[英]Reimplementing Enum.HasFlag for use in Mono 2.6

I'm trying to convert Jint for use in Mono 2.6. 我正在尝试将Jint转换为在Mono 2.6中使用。 Unfortunately Mono 2.6 doesn't have Enum.HasFlag and that's something they use in Jint. 不幸的是,Mono 2.6没有Enum.HasFlag,这就是他们在Jint中使用的东西。 I should also add that I'm quite new to C#. 我还应该补充一点,我对C#还是很陌生。

According to the MSDN page ( http://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx ) the implementation should be 根据MSDN页面( http://msdn.microsoft.com/zh-cn/library/system.enum.hasflag(v=vs.110).aspx ),实现应为

thisInstance And flag = flag

but this doesn't seem to make a lot of sense. 但这似乎没有多大意义。 If these are all bitwise operations shouldn't it be more like this? 如果所有这些都是按位运算,岂不是更像这样吗?

thisInstance & flag == flag

So, the line I'm trying to modify is 因此,我要修改的行是

Writable = !fieldInfo.Attributes.HasFlag(FieldAttributes.InitOnly);

I've stuck this 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 either of these. 可以理解,编译器不喜欢这两个。 For hasFlag1 I get 对于hasFlag1我得到

Operator '&' cannot be applied to operands of type 'System.Reflection.FieldAttributes' and 'bool'

And for hasFlag2: 对于hasFlag2:

Unexpected symbol 'And'

Just want to know if anyone knows how this is meant to be done. 只想知道是否有人知道该怎么做。

Thanks! 谢谢!

It seems based on the error of the compiler that == takes precedence over &. 似乎基于编译器的错误,==优先于&。 Therefore your line is evaluated like this: var hasFlag1 = thisInstance & (thisFlag == thisFlag); 因此,您的行将按以下方式进行评估:var hasFlag1 = thisInstance&(thisFlag == thisFlag);

What you want is this: 您想要的是:

var hasFlag1 = (thisInstance & thisFlag) == thisFlag;

So if you add the parentheses, the compiler error should go away. 因此,如果添加括号,则编译器错误应消失。

Most probably And is the VB equivalent of & 最有可能And是VB等价的&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM