473,321 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

set property in reflection

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.
Nov 21 '05 #1
5 6460
"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/>

Nov 21 '05 #2
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
"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/>

Nov 21 '05 #3
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" wrote:
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
"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/>


Nov 21 '05 #4
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" <De****@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
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" wrote:
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
> "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/>


Nov 21 '05 #5
Thanks.

"JimM" wrote:
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" <De****@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
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" wrote:
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oc**************@TK2MSFTNGP14.phx.gbl...
> "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/>


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
4
by: Bill Borg | last post by:
Hello, I've got a simple shared property, e.g. Public Class dbObject Private Const m_ID As String = "ID" Public Shared ReadOnly Property ID() As String Get Return m_ID End Get End Property
8
by: Rlrcstr | last post by:
If I have some code behind a form and I want to be able to pass an object to it... any of several user defined classes that I have... and then display the values in the member variables of the...
4
by: Pritcham | last post by:
Hi all I've got a number of classes already developed (basic entity classes) like the following: Public Class Contact Private _firstname as String Private _age as Integer Public Property...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.