如何在编辑器工具提示或智能感知上将字节枚举格式化为十六进制数? [英] How to format a Byte Enum as a hexnumber on the Editor Tooltip or Intellisense?

查看:56
本文介绍了如何在编辑器工具提示或智能感知上将字节枚举格式化为十六进制数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在试图弄清楚如何在编辑器工具提示或智能感知中显示或设置我的枚举格式为十六进制?我正在构建一个可重用的类,我想在VB.NET编辑器中引用枚举时显示十六进制数而不是十进制数。



这里是我的一个枚举:

Hi,

I'm trying to figure out how do I display or set the format of my enum to hex on the Editor Tooltip or Intellisense? I'm building a reusable class and I want the hex numbers to show up, instead of the decimal, when referencing the enum within the VB.NET editor.

Here is one of my enums:

Public Enum OutputPort As Byte
    PortA = &H0&
    PortB = &H1&
    PortC = &H2&
    AllPorts = &HFF&
End Enum





例如,当我在VB.NET中键入以下内容 OutputPort.a



自动完成框出现,如果您将鼠标悬停在AllPorts枚举条目上,则会出现一个白色信息框(编辑工具提示或智能感知),显示OutputPort.AllPorts = 255。有没有办法让它显示OutputPort.AllPorts =& HFF&或等效,以便开发人员获取十六进制代码?



这只是许多字节枚举的一个示例,必须以十六进制格式化。我希望有一些.NET格式化属性,而不是评论每个枚举值。



我想要记住我在某个地方看到过这个,但是我在正确的术语上有点生疏,所以我一直无法在谷歌找到有用的结果。



谢谢,



Marco



[本页提供全面解答问题的文章发表于: PropertyGrid和Visual Studio的按位枚举编辑器 [ ^ ] - SA]



For instance, when I type the following in VB.NET OutputPort.a

An autocomplete box comes up, if you hover over the "AllPorts" enum entry, a white info box (Editor tooltip or intellisense) appears that says "OutputPort.AllPorts = 255". Is there a way to get it to display "OutputPort.AllPorts = &HFF&" or equivalent so that the developer gets the hex code instead?

This is just one example of many byte enums that have to be formatted in hex. I'm hoping for some sort of .NET formatting property, instead of commenting each enumerated value.

I want to remember that I have seen this before somewhere, but I'm a bit rusty on the correct terminology, so I have been unable to find useful results in Google.

Thanks,

Marco

[The article providing comprehensive answer to the questions on this page is published: Bitwise Enumeration Editor for PropertyGrid and Visual Studio[^] — SA]

推荐答案

据我所知,你不能在Visual Studio IDE中做到这一点(也许我错了)。



简单的解决方案:让开发人员学习十六进制数字毕竟它是一个有用的知识。



解决方法:使用十六进制数字的字符串表示,并在实际发送之前将它们转换为字节值设备。
As far as I know you cannot do that in Visual Studio IDE (maybe I am wrong).

Simple solution: make the developers learning hexadecimal numbers, after all it is a useful piece of knowledge.

A workaround: use the string representation of the hexadecimal numbers, and convert them to their byte values before actually sending it to the device.


bigbro_1985写道:
bigbro_1985 wrote:

但我想你刚回答了我的另一个问题,或者给了我一个想法。我实际上想要一个按位枚举来分配布尔值,现在我做了一个按位结构,将8位布尔值转换为字节。但是我不喜欢我做的事情,对我来说看起来并不干净。这就是我喜欢的嵌入式开发,因为你得到了所有的按位运算符。

But I think you just answered another problem that I had, or gave me an idea rather. I actually wanted a bitwise enumeration to assign Booleans, right now I made a bitwise structure that converts the 8 bit Boolean value to byte. But I don't like what I did, it doesn't look clean to me. This is what I like about embedded development because you get all your bitwise operators.

哦,这是我前一段时间解决的另一个问题。解决方案非常全面。请参阅我的简短枚举系列中的第一篇文章:​​枚举类型做不列举!解决.NET和语言限制 [ ^ ]。



本文一次解决了一系列不同的相关问题,首先,问题是使用枚举的 foreach (默认情况下,它不是在.NET中完成的,这是一种遗憾),如果成员具有相同的基础整数值,则会产生相当棘手的问题。



其中一个问题是按位操作。我的解决方案有两个方面:首先我在2背景:枚举类之前的生活一节中解释如何手动工作,小节:2.4迭代位集。后来,我解释了我的泛型类Enumeration带来的进步。所以,你可以选择并且可以决定使用简单的技术,如果它对你来说足够了。







你可能无法弄清楚如何读取/分配位作为布尔值。让我们这样做:你阅读我的文章,在这里我将解释其余的,关于布尔。想法是使用索引属性

Oh, this is another problem I solved a while ago. The solution is really comprehensive. Please see my first article from my short "enumeration" series: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

This article solves a set of different related problems at once, first of all, the problem is using foreach for enumerations (it's a shame that it is not done in .NET by default), and a pretty tricky problems if members with identical underlying integer values.

And one of the problems is the "bitwise" operations. My solution is two-fold: first I explain how to work "manually", in the section "2 Background: Life before Enumeration Class", subsection: "2.4 Iterating Bit Sets". And later on, I explain the advances brought by my generic class "Enumeration". So, you will have a choice and may decide to use the simple technique, if it's enough for you.



It's possible that you cannot figure out how to read/assign bits as Boolean. Let's do it: you read my article, and here I'll explain the rest, about Boolean. The idea is to use indexed property:

public enum OptionSet {
    None = 0,
    AlignX = 1 << 0,
    AlignY = 1 << 1, // this is better than 1, 2, 4, 8...
    Transparent = 1 << 2,
    Border = 1 << 3,       
} //enum OptionSet

public class SomeContainerWithEnumerationProperty {

    OptionSet optionSet; // data is here

    public bool this[OptionSet index] {
        get {
            // read bit depending on index
            // and return (bit is set) boolean value
        }
        set {
            // set the bit,
            // depending on index and boolean value
        }
    }

} //class SomeContainerWithEnumerationProperty

//... usage:

SomeContainerWithEnumerationProperty myOptions =
   new SomeContainerWithEnumerationProperty();

myOptions[OptionSet.Border] = true; // set one bit
myOptions[OptionSet.AlignX | OptionSet.AlignX] = true; // set 2 bits at once

//...

myOptions[OptionSet.AlignX] = false; // clear one bit





我希望你能得到它。一些有点棘手的问题是:在这种语法中,你只能有一个这样的属性。你能不止一个人吗?原则上,如果属性类型总是 bool ,但类型如果 index 不同(不同 enum 类型或其他不同的类型),编译器可以解析使用哪一个,但是如何编写呢?



这个想法是:将这些属性放在一个或多个单独的接口中,并使用显式接口实现语法使您的类实现所有这些接口。所以,一个属性是类本身的this,如上所示,加上一个或多个来自接口的实现。



-SA



I hope you got it. Some a bit tricky problem is: in this syntax, you can have only one such property. Can you have more than one? In principle, if property type is always bool, but the types if index different (different enum types or other different types), the compiler can resolve which one to use, but how to write it?

The idea is: put these properties in one or more separate interfaces and make your class implement all these interfaces, with explicit interface implementation syntax. So, one property is "this" of the class itself, as shown above, plus one or more coming from the implementation of interfaces.

—SA


根本不是问题。您可以通过获取枚举变量的值并将其强制转换为byte(在您的情况下)来获取带枚号的下划线整数值。他们将这个字节格式化为十六进制,使用 byte.ToString(字符串格式)格式字符串X,或x或者说X2:

http://msdn.microsoft.com /en-us/library/y11056e9%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx [< a href =http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ] 。



-SA
Not a problem at all. You take enumeration underlined integer value by taking a value of a enumeration variable and casting it to byte (in your case). And them you format this byte as hexadecimal, using byte.ToString(string format) with format string "X", or "x" or, say, "X2":
http://msdn.microsoft.com/en-us/library/y11056e9%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx[^].

—SA


这篇关于如何在编辑器工具提示或智能感知上将字节枚举格式化为十六进制数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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