lundi 20 juin 2016

Setting Values Using Reflection

I am using VB.NET. I have created a small scale test project that works similar to my program. I am trying to say something like: getObjectType(object1) if Object1.getType() = "ThisType" then get the properties. Each object contains an ID and I would like to do this: Object1.Id = -1 (I know it won't be that short or easy). I thought there was a way to do this by using something like: Object1.SetValue(Value2Change, NewValue) but that doesn't work and I'm not sure how to exactly do this. Below is my code. Thank You!

Module Module1

Sub Main()

    Dim Db As New Luk_StackUp_ProgramEntities

    Dim Obj1 As IEnumerable(Of Stackup) = (From a In Db.Stackups).ToList
    Dim Obj2 As IEnumerable(Of Object) = (From a In Db.Stackups).ToList

    Dim IdNow As Integer = Obj1(0).IdStackup
    Dim StackUpNow As Stackup = (From a In Db.Stackups Where a.IdStackup = IdNow).Single
    Console.WriteLine(StackUpNow)

    getInfo(StackUpNow)
    getInfo(Obj1(0), Obj1(0))
    areObjectsSame(Obj1(0), Obj1(67))
    switchObjects(Obj1(0), Obj2(1))
    getObjectValues(Obj2(55))


    Console.WriteLine("========================================")
    TestCopyObject(StackUpNow)
    ChangeObjectValues(StackUpNow)

    Console.ReadKey()
End Sub

Private Sub ChangeObjectValues(Object1 As Object)

    Console.WriteLine("Changing Object Values")
    Dim myField As PropertyInfo() = Object1.GetType().GetProperties()
    'Dim Index As Integer   'Did not find value
    'For Index = 0 To myField.Length - 1
    '    If myField(Index).ToString.Trim = "IdStackup" Then
    '        Console.WriteLine("Found the ID")
    '    End If
    'Next
    If Object1.GetType().Name = "Stackup" Then
        'Set the Value
    End If

End Sub





Aucun commentaire:

Enregistrer un commentaire