set property in reflection

J

JimM

how do you set property using Activator.CreateInstance?

Dim ty As Type = Type.GetTypeFromProgID("Project1.Class1")

Dim o As Object = System.Activator.CreateInstance(ty)

Dim pi As PropertyInfo = ty.GetProperty("Test")

Dim params() As Object = {1S}

pi.SetValue(o, "Test", params) <---- failed here

it failed the last line "Object reference not set to an instance of an
object". Please advice.
 
H

Herfried K. Wagner [MVP]

JimM said:
how do you set property using Activator.CreateInstance?

Dim ty As Type = Type.GetTypeFromProgID("Project1.Class1")

Dim o As Object = System.Activator.CreateInstance(ty)

Dim pi As PropertyInfo = ty.GetProperty("Test")

Dim params() As Object = {1S}

pi.SetValue(o, "Test", params) <---- failed here

it failed the last line "Object reference not set to an instance of an
object".


Maybe you are missing the right 'BindingFlags' in the call to 'GetProperty'
and thus this method returns 'Nothing'.

\\\
Public Class Foo
Private m_Property1 As String
Private m_Property2 As String
Private m_Property3 As Integer

Public Property Property1() As String
Get
Return m_Property1
End Get
Set(ByVal Value As String)
m_Property1 = Value
End Set
End Property

Public Property Property2() As String
Get
Return m_Property2
End Get
Set(ByVal Value As String)
m_Property2 = Value
End Set
End Property

Public Property Property3() As Integer
Get
Return m_Property3
End Get
Set(ByVal Value As Integer)
m_Property3 = Value
End Set
End Property
End Class

Private Sub SetAllStringPropertiesToFoo(ByVal Obj As Object)
For Each p As PropertyInfo In Obj.GetType().GetProperties( _
BindingFlags.Instance Or BindingFlags.Public _
)
If p.PropertyType Is GetType(String) Then
p.SetValue(Obj, "Foo", Nothing)
End If
Next p
End Sub
..
..
..
Dim foo As New Foo
With foo
.Property1 = "X"
.Property2 = "Y"
.Property3 = 22
SetAllStringPropertiesToFoo(foo)
MsgBox(.Property1)
MsgBox(.Property2)
MsgBox(.Property3.ToString())
End With
///
 
J

JimM

I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.
 
G

Guest

Do you have an example of using the InvokeMethod to set or get property? I
would be curious as to how to do it. Thanks.

JimM said:
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.
 
J

JimM

Dim ty As Type = Type.GetTypeFromProgID("ProgID")

Dim o As Object = System.Activator.CreateInstance(ty)

ty.InvokeMember("YourMethodName", BindingFlags.Default Or
BindingFlags.SetProperty, Nothing, o, New Object() {"YourParam"})

If you want to use Get, set BindingFlags to GetPropery



Dennis said:
Do you have an example of using the InvokeMethod to set or get property?
I
would be curious as to how to do it. Thanks.
 
G

Guest

Thanks.

JimM said:
Dim ty As Type = Type.GetTypeFromProgID("ProgID")

Dim o As Object = System.Activator.CreateInstance(ty)

ty.InvokeMember("YourMethodName", BindingFlags.Default Or
BindingFlags.SetProperty, Nothing, o, New Object() {"YourParam"})

If you want to use Get, set BindingFlags to GetPropery
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top