标志枚举与多个零值的问题(TextFormatFlags) [英] Flags enumeration with multiple zero values problem (TextFormatFlags)

查看:130
本文介绍了标志枚举与多个零值的问题(TextFormatFlags)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图写一个自定义的控制,我遇到一个问题,与System.Windows.Forms.TextFormatFlags枚举结合在Visual Studio(2005/2008)编辑器。这样做的原因的问题似乎是来自该该枚举具有多个成员哪个映射到零的值的事实。选择这件(GlyphOverhangPadding,左,默认,顶部)的结果在编辑器中的属性设置为

While trying to write a custom control I've come across a problem with the System.Windows.Forms.TextFormatFlags enum in combination with the Visual Studio (2005/2008) editor. The reason for this problem seems to come from the fact that this enum has multiple members which map to a zero value. Selecting any of these members (GlyphOverhangPadding, Left, Default, Top) results in the editor setting the property to

this.customControl.TextFormatFlags = System.Windows.Forms.TextFormatFlags.GlyphOverhangPadding;

在code编译,符合市场预期。然而,从编辑器的属性网格结果在下面选择非零成员(例如右):

The code compiles, as expected. However, selecting any non-zero member (e.g. "Right") from the editor's property grid results in the following:

this.customControl.TextFormatFlags = System.Windows.Forms.TextFormatFlags.Left, Default, Top, Right;

显然,这不能编译。选择一个以上的非零成员(通过UITypeEditor的,比如右|底)的结果如下:

Obviously this does not compile. Selecting more than one non-zero member (Through a UITypeEditor, e.g. "Right | Bottom") results in the following:

this.customControl.TextFormatFlags = ((System.Windows.Forms.TextFormatFlags)((System.Windows.Forms.TextFormatFlags.Left, Default, Top, Right | System.Windows.Forms.TextFormatFlags.Left, Default, Top, Bottom)));

正如你所看到的,在编辑器添加了三个四个零值成员的任何选择的项目。

As you can see, the editor adds three of the four zero-value members to any selected item.

如果你想重现此问题:

  • 创建在Visual Studio 2005/2008一个新的项目(Windows窗体应用程序)
  • 添加自定义控制到项目中。
  • 添加私人领域和公共财产的新类:

  • Create a new project in Visual Studio 2005/2008 (Windows Forms Application)
  • Add a Custom Control to the project.
  • Add a private field and public property to the new class:

私人TextFormatFlags TFF = TextFormatFlags.Default;

private TextFormatFlags tff = TextFormatFlags.Default;

公开TextFormatFlags TFFProperty {     {返回this.tff; }     集合{this.tff =价值; } }

public TextFormatFlags TFFProperty { get { return this.tff; } set { this.tff = value; } }

编译code

如果你创建自己的枚举的标志属性,并添加映射到零多个成员同样的情况(这是一种畸形的标志枚举?)。我已经验证这是不是与我使用的UITypeEditor的一个错误(同样的问题时不使用UITypeEditor的)。我试着用一个转换器绕过这个问题,但至今没有成功。如果任何人有关于如何解决这个问题的任何想法,我会很高兴听到他们的声音。

The same happens if you create your own enum with the Flags attribute and add more than one member mapped to zero (which is a kind of malformed flags enum?). I've verified that this is not a bug with the UITypeEditor I'm using (The same problem occurs without using the UITypeEditor). I've tried to circumvent the problem with a Converter, so far without success. If anyone has any ideas on how to solve this problem I'd be glad to hear them.

推荐答案

我通过各个阶级检查 System.ComponentModel.Design.Serialization 使用反射命名空间,我认为,codeDOM串行器是一个有点调皮。

I checked through the various classes in the System.ComponentModel.Design.Serialization namespace using Reflector, and I think the CodeDom serializer is being a bit naughty.

枚举获得由枚举codeDomSerializer.Serialize 处理,其目的是把一个枚举,并把它变成一个系统。codeDOM。codeEX pression 对象重新您在设计文件中看到presents。

Enums get handled by EnumCodeDomSerializer.Serialize, whose purpose is to take an enum and turn it into a System.CodeDom.CodeExpression object that represents what you see in the designer file.

此方法正确使用 codeBinaryOperatorEx pression 办理 | 前$的方面p $ pssion。然而,对于个别枚举值,它使用了 Enum.ToString 通过 EnumTypeConverter ,直接粘导致串入前pression树。

This method correctly uses CodeBinaryOperatorExpression to handle the | aspect of the expression. However, for the individual enum values, it uses Enum.ToString via EnumTypeConverter and sticks the resulting string directly into the expression tree.

我觉得 Enum.ToString 是你看到的最终原因:

I think Enum.ToString is the ultimate cause of what you're seeing:

如果多个枚举成员具有相同的潜在价值,并尝试获取字符串再枚举成员的名字是根据它的潜在价值presentation,你的code不应该做出哪些命名方法,任何假设会回来。

If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return.

不可否认的MSDN页面上的 Enum.ToString 不谈论逗号,但它仍然看起来并不安全依赖于<$ C $的输出C> Enum.ToString 是一个有效的C#EX pression。

Admittedly the MSDN page on Enum.ToString doesn't talk about the commas, but it still doesn't seem safe to rely on the output of Enum.ToString being a valid C# expression.

我不知道这是什么意思为您的控件:

I'm not sure what this means for your control:

  • 很明显,你可以定义自己的替换 TextFormatFlags ,做它没有重复零标志
  • 您可能能够使用自定义类型转换器本事,说不定要转换为 InstanceDescriptor 。这给你更多的控制在什么出现在设计器生成的code。
  • 您可能可能暴露一个 INT 设计师串行但 TextFormatFlags 的属性网格
  • Clearly you can define your own replacement for TextFormatFlags and do it without the duplicated zero flags
  • You may be able to hack it with a custom type converter, maybe one that converts to InstanceDescriptor. This gives you a little more control over what appears in the designer-generated code.
  • You could possibly expose an int to the designer serializer but a TextFormatFlags to the property grid

编辑: 逗号分隔列表的行为 Enum.ToString ,其实是在记录

The comma-separated list behaviour of Enum.ToString is in fact documented

这篇关于标志枚举与多个零值的问题(TextFormatFlags)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆