数日間の調査の後、解決できない問題があります...
他のアプリケーションをテストするために、Excel ファイルを読み取り、列の名前と値に応じてオブジェクトを生成するアプリケーションを作成する必要があります。その部分に問題はありません。型を知らなくても、値を作成してオブジェクトに割り当てることができます。
私が抱えている問題は、オブジェクト A に List(Of Object 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
デバッグすると、次のような結果が得られます: Person: Name = "Something", Age = 30, Dogs = Nothing
私の List(Of T) は何にも等しいので、オブジェクトを追加する前にインスタンス化する必要があります。リストにそれ自体をインスタンス化するように指示する方法がまだ見つからないため、新しいリストをプロパティに割り当てようとしました。
私が試したいくつかのコードがあります:
'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
また、テストする必要があるオブジェクトのライブラリを受け入れることができるはずなので、オブジェクトを変更することはできません。
これは可能ですか?私はその問題を解決する必要があります。前もって感謝します
PS 私の英語が下手で申し訳ありません。それは私の母国語ではありません。