"JimM" <Ji**@hotmail.com> schrieb:
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
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>