mercredi 19 août 2020

Dynamically generated generic type not working properly

the following code works without any issue:

 Dim SubmodelElement = New BaSyx.Models.Core.AssetAdministrationShell.Implementations.SubmodelElementTypes.[Property](Of Double)() With {
                .IdShort = "TestProperty1",
                .[Set] = Function(prop, val)
                             Return _test = val
                         End Function
        }
    SubmodelElement.[Get] = Function(prop)
                                Return 4711
                            End Function

What I am trying to do is to create those submodel elements dynamically for each property of my class with the following code:

For Each Eigenschaft As PropertyInfo In Me.GetType().GetProperties()
            If Eigenschaft.CanRead = True Then
                Dim Eigenschaftstyp As Type = Type.GetType(Eigenschaft.PropertyType.FullName)

                Dim GenerischerTypBasysProperty As Type = GetType(BaSyx.Models.Core.AssetAdministrationShell.Implementations.SubmodelElementTypes.Property(Of)).MakeGenericType(Eigenschaftstyp)
                Dim BasysEigenschaft = Activator.CreateInstance(GenerischerTypBasysProperty)

                BasysEigenschaft.IdShort = Eigenschaft.Name
                BasysEigenschaft.[Get] = Function(prop)
                                             'Return Eigenschaft.GetValue(Me)
                                             Return 4711
                                         End Function
                BasysEigenschaft.Set = Sub(ByVal Prop, ByVal Wert)
                                           Eigenschaft.SetValue(Me, Wert)
                                       End Sub
                MyBase.SubmodelElements.Add(BasysEigenschaft)
            End If
        Next

As seen in the debugger both objects (SubmodelElement and BasysEigenschaft are of same type).

screenshot of values of submodelElement and BasysEigenschaft

Unfortunally the assigning of the [Get]-function, doesnt work in the second code snipet. I get this error

System.InvalidCastException: "Method invocation failed because 'Public Overrides Property Get() As BaSyx.Models.Core.AssetAdministrationShell.Generics.SubmodelElementTypes.GetPropertyValueHandler(Of Double)' cannot be called with these arguments: Argument matching parameter 'value' cannot convert from 'VB$AnonymousDelegate_0(Of Object,Integer)' to 'GetPropertyValueHandler(Of Double)'."

screenshot of error message

Any help will be greatly appreciated





Aucun commentaire:

Enregistrer un commentaire