Winforms中的Tag属性有什么用?
我注意到带有控件的标签属性。是否可以使用它来引用我的自定义对象,或者我应该远离它,因为它需要装箱和拆箱,这已被提及为不安全且不建议。
TreeNode tn = new TreeNode();
CustClass o = new CustClass()
o.number = 123;
tn.Tag = o;
class CustClass
{
public int number {get; set;}
}
I have noticed the Tag properties with controls. Is it okay to use this to reference my custom objects, or should I stay away from it as it would require boxing and unboxing which has been mentioned as unsafe and is not recommended.
TreeNode tn = new TreeNode();
CustClass o = new CustClass()
o.number = 123;
tn.Tag = o;
class CustClass
{
public int number {get; set;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Tag
属性的目的是让您可以将其用于您想要的任何目的。您可以安全地将任何您想要的东西存放在那里。通常会声明一个专门用于存储在
Tag
属性中的类(就像您对CustClass
所做的那样)。但如果你只需要其中一个值,那么直接在其中存储一个int
并没有什么问题。您在哪里读到装箱/拆箱是“不安全”的?这绝对不是真的。 (有些人声称它效率低,但事实并非如此。)此外,您的代码甚至根本不包含装箱示例。
CustClass
是一个引用类型。只有值类型在分配给object
时才会被装箱。The purpose of the
Tag
property is for you to use it for any purpose you want. You can safely store anything in there you want.It is customary to declare a class that is specifically intended for being stored in the
Tag
property (like you did with yourCustClass
). But if you need only one value in it, then there is nothing wrong with storing anint
in it directly.Where did you read that boxing/unboxing is “unsafe”? That is absolutely not true. (Some people claim that it is inefficient, but even that is not true.) Furthermore, your code does not even contain an example of boxing at all.
CustClass
is a reference type. Only value types are boxed when assigned toobject
.我认为按照您想要的方式使用它不会成为装箱或拆箱问题。据我所知,该 Tag 属性仅供程序员使用,因此您可以按照您需要的方式使用它。
请参阅
Control.Tag
MSDN 上的属性,以进一步参考如何使用此属性。例如,我曾经使用它在 Windows 窗体应用程序中向用户输入指令。当控件 GotFocus 事件触发时,指令 Label.Text 属性被分配了包含指令字符串的控件 Tag 属性的值。
I don't think this would be a boxing or unboxing problem to use it the way you want. As far as I know, that Tag property is for the programmer's purpose only, so you can use it quite the way you need it to be used.
Please see
Control.Tag
property on MSDN for further reference on how to use this property.For instance, I used to use it to input instructions to the user in Windows Forms applications. When the control GotFocus event triggered, the instructions Label.Text property was assigned the value of my control Tag property which contained the instruction string.
来自其他人也提到的链接:
在 https: //msdn.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx
其中写道:
以及 https://msdn.microsoft.com /en-us/library/system.windows.forms.treenode.tag.aspx 内容如下:
From links also mentioned by others here:
at https://msdn.microsoft.com/en-us/library/system.windows.forms.control.tag.aspx
one reads:
and at https://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx one reads:
好吧,您可以创建自己的 TreeNode 派生类:
但是,当您从树中检索节点时,您将进行强制转换,这比强制转换 Tag 属性没有任何改进。并且您应该重写 Clone() 方法。
一种更简洁的方法是利用 TreeNode.Name 并将其作为字典中的键来查找自定义数据。当 CustClass 变得不平凡时,这很好。 Name 属性不用于其他任何用途。
Well, you could create your own TreeNode derived class:
But then you'll be casting when you retrieve the node from the tree, no improvement over casting the Tag property. And you ought to override the Clone() method.
A cleaner approach is to leverage TreeNode.Name and make that a key in a dictionary to find your custom data back. Good when CustClass gets to be non-trivial. The Name property isn't used for anything else.