类型字典的属性 [英] Property of type dictionary
问题描述
我正在使用这个用户控件通过设计器添加属性。
我尝试了以下内容:
私人v作为字典(字符串,字符串)
公共属性VList As Dictionary(Of String,String)
获取
返回v
End Get
Set(ByVal value As Dictionary(Of String,String))
v = value
结束集
结束属性
但是,当我尝试这个字符串集合编辑器窗口打开,但添加&删除按钮被禁用。声明此属性的正确方法是什么?
我想添加键&
字典
没有建立在 UITypeEditor
中。有很多原因为什么没有:有2种类型是通用的,它也没有一个项目
访问器,没有简单的添加
方法,密钥必须是唯一的,没有内置的序列化字典项目的方式。
/ em>方式是使用继承自 Collection< T>
的Collection类,以便您可以控制对内容的访问(注意:这是来自系统.Collections.ObjectModel
不可怕的VB集合!)。设置工作界面的快速方式是使用 List(Of myTypeClass)
,但这在生产代码中是危险的,因为它允许所有种类在您可能不想要的内容列表中的操作。
< Serializable>< TypeConverter (的GetType(FooConverter))GT;
公共类FooBar
< DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
公共属性名称为String
< DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
公共属性值作为字符串
'简单ctor为UITypeEditor需要
Public Sub New()
Name =
Value =
End Sub
'TypeConverter(不包括)
Public Sub New(n As String,v As String)
名称= n
值= v
End Sub
公共覆盖函数ToString
返回名称
End Sub
结束类
'必须实例化
私人myFoo作为新列表(FooBar)
'list是一个对象,所以它不能被序列化,但CONTENTS可以是
< DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> ;
公共属性FooList作为列表(FooBar)
获取
如果myFoo不是,然后
myFoo =新列表(FooBar)
结束如果
返回myFoo
结束Get
设置
'do nothing
结束集
End Sub
'用于设计器序列化
私有函数ShouldSerializeFooList As Boolean
返回myFoo.Count> 0'或myFoo IsNot Nothing
End Sub
public Sub ResetMyFolist
myFoo =新列表(FooBar)
End Sub
注意事项:
几乎总是更好地为Foobar项目写一个类容器。通常你会从 Collection< T>
继承。 列表< T>
如图所示是一个容器和一个集合,所以内容可以被清除,重置,修改等,如图所示。他们是快速和容易实现虽然,基本的概念是一样的。
如果一个字典真的是你想要的,你可以编写自己的 UITypeEditor
(不是UIDesigner,这不是一个控件),但这可能需要在很多层面上进行大量的工作。没有他们飞过的原因是大多数人使用标准的收藏品之一做,并以其他方式强制执行独特的名称。 (将属性添加到用户控件中,表明真正的密钥
或名称应该在应用程序提前修复和知道应用程序,因此知道它是什么,什么这样做(?))。
通常,VS可以使用简单的属性来执行设计器序列化,如 FooBar
。但是,由于它们是集合中的项目,因此您可能需要编写一个 TypeConverter
,它可以返回一个 InstanceDescriptor
帮助VS实例。但这是一个不同的问题。
Is there a way to declare a property of type dictionary of string, string in VB.Net. I am using this on a usercontrol to add properties via the designer.
I tried the following:
Private v As Dictionary(Of String, String)
Public Property VList As Dictionary(Of String, String)
Get
Return v
End Get
Set(ByVal value As Dictionary(Of String, String))
v = value
End Set
End Property
But when I try this the string collection editor window opens up but the add & remove buttons are disabled. What is the correct way to declare this property?
I want to add the key & value via the designer.
The Dictionary
does not have a built in UITypeEditor
. There are many reasons why there isn't: there are 2 Types which are generic, it also doesnt have an Item
accessor, there is no simple Add
method, the key must be unique and there is no built in way to serialize a Dictionary "item".
The right way is to use a Collection class inheriting from Collection<T>
so you can control access to the contents (note: this is from System.Collections.ObjectModel
not the horrible VB Collection!). The fast way to setup a working interface is to use a List(Of myTypeClass)
, but this is dangerous in production code because it allows all sorts of actions on the innerlist which you likely do not want.
<Serializable><TypeConverter(GetType(FooConverter))>
Public Class FooBar
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property Name As String
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property Value As String
' simple ctor REQUIRED for the UITypeEditor
Public Sub New()
Name = ""
Value = ""
End Sub
' ctor for the TypeConverter (NOT included)
Public Sub New(n As String, v As String)
Name = n
Value = v
End Sub
Public Overrides Function ToString
Return Name
End Sub
End Class
' must be instanced
Private myFoo As New List(Of FooBar)
' list is an object so it cant be serialized, but the CONTENTS can be
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Public Property FooList As List(of FooBar)
Get
If myFoo Is Nothing Then
myFoo = New List(of FooBar)
End If
Return myFoo
End Get
Set
' do nothing
End Set
End Sub
' for designer serialization
Private Function ShouldSerializeFooList As Boolean
Return myFoo.Count > 0 ' or myFoo IsNot Nothing
End Sub
public Sub ResetMyFolist
myFoo = New List(of FooBar)
End Sub
Caveats:
It is almost always better to write a class container for the Foobar items. Usually you would inherit from Collection<T>
. List<T>
as shown is a container and a collection, so the contents can be cleared, reset, modified etc when exposed as shown. They are fast and easy to implement though and the basic concept is the same.
If a Dictionary is really what you want, you can write your own UITypeEditor
(not UIDesigner, this is not a control) but this would probably require a great deal of work on many levels. The reason there are not gobs of them flying around is that most people make do with one of the standard collections and simply enforce unique names in other ways. (Adding "Properties" to a usercontrol, suggests that really the key
or name ought to be fixed and known to the app ahead of time so it knows what it is and what to do with it(?)).
Often VS can perform designer serialization on its own with simple properties like those in FooBar
. However, since they are items in a collection, you will likely need to also write a TypeConverter
which can return an InstanceDescriptor
, to help VS instance them. But that is a different question.
这篇关于类型字典的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!