MattCucco
MattCucco

Reputation: 133

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

Upvotes: 1

Views: 4318

Answers (2)

Mark
Mark

Reputation: 8150

You can use PropertyInfo.SetValue to set the value using reflection. You could also use a LINQ SingleOrDefault query to simplify finding the correct PropertyInfo, so you could do something like this:

Private Sub ChangeObjectValues(Object1 As Object)

    Console.WriteLine("Changing Object Values")

    Dim t As Type = Object1.GetType()
    If t.Name = "Stackup" Then
        Dim myField As PropertyInfo = t.GetProperties() _
            .SingleOrDefault(Function(x) x.Name = "IdStackup")
        If myField IsNot Nothing Then
            Console.WriteLine("Found the ID")
            myField.SetValue(Object1, -1)
        End If
    End If

End Sub

It's not clear from the question if you really need to use reflection - perhaps a common interface to define the id property, or just type checking and casting, etc. would be better.

Upvotes: 2

Bert Cushman
Bert Cushman

Reputation: 801

Well, I struggled to see how your code example applied to your question, but if you are simply asking how to set the ID of an object using reflection, this code might help you. The trick is that a property is typically handled using a set and a get method.

Imports System.Web.UI.WebControls
Imports System.Reflection

Module Module1

Sub Main()
    Dim tb As New Label()

    Dim t As Type = tb.GetType()
    If TypeOf tb Is Label Then
        Dim mi As MethodInfo = t.GetMethod("set_ID")
        mi.Invoke(tb, New Object() {"-1"})
    End If

    Console.WriteLine(tb.ID)
    Console.ReadLine()
End Sub

End Module

Upvotes: 1

Related Questions