473,394 Members | 1,870 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,394 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 1001
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rakesh | last post by:
Hi, I am able to obtain a MenuItem object's Name property @ design-time, but am not able to get the same @ run- time...why? And since MenuItem doesn't inherit from Control class, it's not...
2
by: Jim Heavey | last post by:
I want to write a routine which will list all of the propetries and all property names for a particular object, say a DataColumn. I there a way for me to do this without manually looking up each...
1
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using...
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
5
by: Tomislav Bartolin | last post by:
Hi, Is it possible to set value to a read-only property via reflection? Thx -- Bartol -- Remove "TB" from address to reply
3
by: George Meng | last post by:
Hi folks, I have a Treeview control in the Winform, and I want to change the text of the 5th node base on an expression: dim sObject as string = "Treeview1.Node.Items(5).Text"
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...
1
by: Guido Kraus | last post by:
Hi, I wonder how to get the name of the currently executing property by using reflection in .Net 2.0 If I use System.Reflection.MethodInfo.GetCurrentMethod().Name from within a property I get...
2
by: diego | last post by:
hi everyone, i have a sub that opens a form given the form's name as string and opens it using System.Reflection. How can I set the form's properties at runtime. Here is my code. Public Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.