C# 逻辑与的应用
Type tp = obj.GetType();
FieldInfo propInfo = tp.GetField(strField
, BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.Static
);
namespace System.Reflection
{
[__DynamicallyInvokable, Flags, ComVisible(true)]
[Serializable]
public enum BindingFlags
{
Default = 0,
IgnoreCase = 1,
DeclaredOnly = 2,
Instance = 4,
Static = 8,
Public = 16,
NonPublic = 32,
FlattenHierarchy = 64,
InvokeMethod = 256,
CreateInstance = 512,
GetField = 1024,
SetField = 2048,
GetProperty = 4096,
SetProperty = 8192,
PutDispProperty = 16384,
PutRefDispProperty = 32768,
ExactBinding = 65536,
SuppressChangeType = 131072,
OptionalParamBinding = 262144,
IgnoreReturn = 16777216
}
}
查看 BindingFlags 枚举,结果发现,运用二进制的逻辑与,能够比较方便、高效、易读的 做出 组合 的场景。
例如,10个元素,只有某些元素间组合可用,其余组合都是不合法,那么,可以将 元素 做个二进制进位的枚举,然后做加法,值不会重复,放到集合里;当前组合,判断是否在集合中存在即可;
逻辑与应该有更简单的算法;