c#-System.Reflection.TypeAttributes是否带有FlagsAttribute枚举类型?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-System.Reflection.TypeAttributes是否带有FlagsAttribute枚举类型?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1924字,纯文字阅读大概需要3分钟。
内容图文
![c#-System.Reflection.TypeAttributes是否带有FlagsAttribute枚举类型?](/upload/InfoBanner/zyjiaocheng/666/db82c64819d34c62939defebae251516.jpg)
枚举类型System.Reflection.TypeAttributes显得很病态.它带有[Flags]属性,并且具有不少于四个常量零的同义词.从Visual Studio生成的“元数据”中:
#region Assembly mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.2\mscorlib.dll
#endregion
using System.Runtime.InteropServices;
namespace System.Reflection
{
//
// Summary:
// Specifies type attributes.
[ComVisible(true)]
[Flags]
public enum TypeAttributes
{
//
// Summary:
// Specifies that the class is not public.
NotPublic = 0,
//
// Summary:
// Specifies that class fields are automatically laid out by the common language
// runtime.
AutoLayout = 0,
//
// Summary:
// Specifies that the type is a class.
Class = 0,
//
// Summary:
// LPTSTR is interpreted as ANSI.
AnsiClass = 0,
// (followed by non-zero members...)
为什么有人会在带有FlagsAttribute的枚举类型中使用四个名称来表示零?看起来真的很疯狂.
看一下后果:
var x = default(System.Reflection.TypeAttributes); // 0
var sx = x.ToString(); // "NotPublic"
var y = (System.Reflection.TypeAttributes)(1 << 20); // 1048576
var sy = y.ToString(); // "AutoLayout, AnsiClass, Class, BeforeFieldInit"
在此,类型的零值x的字符串表示形式变为“ NotPublic”.当非零y的字符串表示形式变为“ AutoLayout,AnsiClass,Class,BeforeFieldInit”时.关于y,请注意,它仅具有单个位集(1 << 20),并且名称BeforeFieldInit本身恰好占该位.其他所有三个名称(AutoLayout,AnsiClass,Class)对值的贡献为零.
到底是怎么回事?
为什么要这样设计?
解决方法:
ToString()表示形式基本上无关紧要
当某些选项是非二进制的时,这种模式很常见.例如,有3种可能的选择.在那种情况下,您可以指定2个位来携带这3个选项(不使用4个),而“默认”选项(在逻辑上)为00.这意味着,是的,会有多个0同义词.
注意:如果枚举作者想要使其更加明确,则也可能在纯二进制选项中发生-因为调用者不需要知道哪个“打开”和哪个“关闭”.
基本上,不必担心ToString()
内容总结
以上是互联网集市为您收集整理的c#-System.Reflection.TypeAttributes是否带有FlagsAttribute枚举类型?全部内容,希望文章能够帮你解决c#-System.Reflection.TypeAttributes是否带有FlagsAttribute枚举类型?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。