VS2013,Visual Basic
我有一个有很多属性的类。
Public Class
Property 1
.
.
Property N
End Class
我有一个名称值对列表
name1, value1
.
.
nameN, valueN
名称值对中的值将分配给属性值。
VB是否有办法让我拿一个名字并用它来查找'在class属性中,选择它为它赋值,循环遍历名称 - 值对来进行所有赋值?
在我定义的时候,我没有看到附加到我的班级的方法。我应该以不同方式定义我的班级我使用EF6 Code First方法中的Class来创建后备数据库。
我认为另一种选择是逐个列出每个Class属性,查找名称并赋值,但这似乎是一种繁琐的处理方式。
想到我会问。也许有更好的方法来做到这一点。
感谢。
最诚挚的问候, 艾伦
答案 0 :(得分:0)
有三个课程可以帮助你; TypeDescriptor,PropertyDescriptor和PropertyDescriptorCollection。它们都位于System.ComponentModel命名空间中。
Imports System.ComponentModel
我们将在此示例中使用以下类:
Public Class Foo
'Implements ICustomTypeDescriptor (Optional)
Public Property A() As String
Public Property B() As Date
Public Property C() As Integer
Public Property D() As Boolean
Public Overrides Function ToString() As String
Return String.Format("A='{0}', B='{1}', C='{2}', D='{3}'", Me.A, Me.B, Me.C, Me.D)
End Function
End Class
通过调用GetProperties
类的静态方法TypeDescriptor
来获取所有属性。它返回PropertyDescriptor
类的集合 - 您的属性。然后,您只需调用SetValue
和/或GetValue
方法。请注意,您可以通过实现ICustomTypeDescriptor接口来实现自定义类型描述符。
Private Sub RunTest()
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(Foo))
Dim ignoreCase As Boolean = True
Dim foo1 As New Foo()
properties.Find("A", ignoreCase).SetValue(foo1, "hello")
properties.Find("B", ignoreCase).SetValue(foo1, Date.Now)
properties.Find("C", ignoreCase).SetValue(foo1, 1234I)
properties.Find("D", ignoreCase).SetValue(foo1, True)
'Get property value:
'Dim a As String = CType(properties.Find("A", ignoreCase).GetValue(foo1), String)
Debug.WriteLine(foo1.ToString())
End Sub
输出:(immediate window)
A =' hello',B =' 30.11.2014 11:14:39',C =' 1234',D =' True& #39;
<强>扩展强>
为了进一步扩展,可以创建一些扩展方法。
Imports System.Runtime.CompilerServices
的
Public Module Extensions
<Extension()>
Public Function GetProperty(Of TComponent)(component As TComponent, propertyName As String, Optional ByVal ignoreCase As Boolean = True) As Object
Return TypeDescriptor.GetProperties(GetType(TComponent)).Find(propertyName, ignoreCase).GetValue(component)
End Function
<Extension()>
Public Function GetProperty(Of TComponent, TValue)(component As TComponent, propertyName As String, Optional ByVal ignoreCase As Boolean = True) As TValue
Return CType(TypeDescriptor.GetProperties(GetType(TComponent)).Find(propertyName, ignoreCase).GetValue(component), TValue)
End Function
<Extension()>
Public Sub SetProperty(Of TComponent)(instance As TComponent, propertyName As String, value As Object, Optional ByVal ignoreCase As Boolean = True)
TypeDescriptor.GetProperties(GetType(TComponent)).Find(propertyName, ignoreCase).SetValue(instance, value)
End Sub
End Module
现在按名称设置/获取属性值非常容易。
Private Sub RunTest()
Dim foo1 As New Foo()
foo1.SetProperty("A", "hello")
foo1.SetProperty("B", Date.Now)
foo1.SetProperty("C", 1234I)
foo1.SetProperty("D", True)
'Get property value:
'Dim a As String = CType(foo1.GetProperty("A"), String)
'Dim a As String = foo1.GetProperty(Of String)("B")
Debug.WriteLine(foo1.ToString())
End Sub
输出:
A =&#39; hello&#39;,B =&#39; 30.11.2014 11:18:17&#39;,C =&#39; 1234&#39;,D =&#39; True& #39;