2012-08-13 60 views
0

我有一个问题后,研究的几天里,我解决不了分配泛型类型的List属性...创建和VB.Net

我必须创建一个读取Excel文件的应用程序,根据列的名称和值生成对象以测试其他应用程序。该部分没有问题,我可以在不知道类型的情况下为我的对象创建和赋值。

我遇到的问题是这些对象之间有一些关系,因为对象A有一个List(对象B)。我可以识别这些。

我的对象声明如下:

Public Class Person 
    Public Property Name as String 
    Public Property Age as Integer 
    Public Property Dogs as List(Of Dog) 
    ... 
End Class 

Public Class Dog 
    Public Property Name as String 
    ... 
End Class 

当我debbug,我得到类似的东西: 人:名称= “东西”,年龄= 30,狗=无

由于我List(Of T)等于Nothing,我需要在将对象添加到它之前实例化它。由于我还没有找到任何方式来告诉List实例化自己,我试图给属性分配一个新列表。

我有一些代码,我想:

'In this case, unObjetParent would be a Person, and unObjetEnfant would be a Dog 
Private Function AssocierObjets(ByRef unObjetParent As Object, ByRef unObjetEnfant As Object) As Boolean 
    Dim proprietes() As Reflection.PropertyInfo = unObjetParent.GetType.GetProperties 
    Dim typeEnfant As Type = unObjetEnfant.GetType 

    For Each p As Reflection.PropertyInfo In proprietes 
     If p.PropertyType.Name.Equals("List`1") Then 
      Dim type As Type = p.PropertyType 
      Dim splittedAssemblyQualifiedName As String() = type.AssemblyQualifiedName.Split(","c) 
      Dim typeOfList As Type = type.GetType(splittedAssemblyQualifiedName(0).Substring(splittedAssemblyQualifiedName(0).LastIndexOf("["c) + 1)) 
      '^The way I found to get the Type of the GenericArgument of the List before instantiating it. 

      If typeOfList = typeEnfant Then 

       'This create a object with it's name, in this case, it returns a Dog 
       Dim prototype = DefinirObjet(typeOfList.Name) 

       'My first try, it returns a List(Of VB$AnonymousType_0`1[System.Object]) 
       'which I can't convert into a List(Of Dog(for this example)) 
       Dim prop = New With {unObjetEnfant} 
       Dim l = prop.CreateTypedList 
       p.SetValue(unObjetParent, l, Nothing) 

       'This one doesn't work either as it returns a List(Of Object) 
       'which I can't convert into a List(Of Dog(for this example)) 
       p.SetValue(unObjetParent, CreateTypedList2(prototype), Nothing) 

      End If 
     End If 
    Next 
    Return True 
End Function 

<System.Runtime.CompilerServices.Extension()> _ 
Function CreateTypedList(Of T)(ByVal Prototype As T) As List(Of T) 
    Return New List(Of T)() 
End Function 

Private Function CreateTypedList2(Of T)(ByVal unPrototype As T) As List(Of T) 
    Return New List(Of T) 
End Function 

而且,我不能修改的对象,我应该能够接受我们需要测试对象的任何库。

这可能吗?我需要解决这个问题。在此先感谢

P.S.对不起,如果我的英语不好,这不是我的母语。

+0

我不明白你为什么需要在这里反思。在哪里添加_dogs_到'List(Of Dog)'?如果唯一的问题是列表没有初始化,为什么不初始化呢?例如:'公共财产狗作为新列表(狗)' – 2012-08-13 15:43:57

+0

好吧,正如我所说,我不能改变我的工作班,所以我不能改变公共财产狗列表(狗)公开财产犬作为新名单(狗)。我使用反射来遍历对象的所有属性,因为在运行时我不知道它的类型。我将在它(列表)被初始化之前将它添加到列表中。 – 2012-08-13 15:48:50

+0

* ...之后列表得到初始化,对不起 – 2012-08-13 17:12:23

回答

0

最后,我找到了一种方法做什么,我需要:

 Dim t1 As Type = GetType(List(Of)) 
     Dim constructed As Type = t1.MakeGenericType(typeEnfant) 
     Dim o As Object = Activator.CreateInstance(constructed) 

此创建一个泛型集合的实例,在我的情况下指定类型的List(Of T)已和我我可以将它作为我正在工作的财产。