471,354 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 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.

Jul 21 '05 #1
3 977
JimM <Ji**@hotmail.com> wrote:
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.


Well, you're not checking the return value of GetProperty for whether
it's Nothing or not. I suspect it *is* Nothing, but in order to see
why, we'd need to see the rest of your code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
JimM <Ji**@hotmail.com> wrote:
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.


Well, you're not checking the return value of GetProperty for whether
it's Nothing or not. I suspect it *is* Nothing, but in order to see
why, we'd need to see the rest of your code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
JimM <Ji**@hotmail.com> wrote:
I use InvokeMethod to set or get property and it works fine. This is COM
interop. Thanks for the quick reply.


Ah, if it's interop, that could well be the problem - it may not be
exposed as a property as far as .NET is concerned. Again, without
seeing any code, it's hard to tell.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Rakesh | last post: by
2 posts views Thread by Jim Heavey | last post: by
4 posts views Thread by Bill Borg | last post: by
5 posts views Thread by Tomislav Bartolin | last post: by
3 posts views Thread by George Meng | last post: by
1 post views Thread by Guido Kraus | last post: by

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.