如何将输入数组绑定到对象的属性

时间:2015-02-10 19:35:06

标签: arrays vb.net oop

这是我的问题:

我有一个具有3个属性的 Temp 类。

public class Temp 
    public property attribute1 as string
    public property attribute2 as string
    public property attribute3 as string
end Class

我想将一个字符串数组传递给类的构造函数,并将数组中的每个元素按顺序绑定到一个属性

所以这是我的代码(这假设输入数组d的长度与Temp类中的属性数相匹配)

            'd is the input array of Strings
            Dim temp As New Temp
            Dim propertyinfo() = temp.GetType().GetProperties()
            Dim i As Integer = 0
            For Each proper In propertyinfo
                proper.SetValue(temp, d(i))    
                i = i + 1
            Next

当Temp类的属性都是字符串时,这很好用。但是,如果类型不是字符串(例如Date类型),即使数组的input元素是有效的Date但是以字符串格式,也会发生转换异常。

我的问题是,当我执行此操作时,无论如何都要将d的每个元素自动转换为属性的类型

                proper.SetValue(temp, d(i))    

谢谢

0 个答案:

没有答案
相关问题