473,396 Members | 1,702 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,396 software developers and data experts.

Fairly simple I think

If I have a string representation of a property such as
"MyObject.SomeProperty", how would I then create a "MyObject" object and
invoke "SomeProperty". Any assistance would be appreciated. Thanks.
Jun 23 '06 #1
3 1106
Robert,

Look in the System.Reflection namespace.

Specifically, you have to load the Type instance for MyObject, and then
pass that to the static CreateInstance method on the Activator class to
create an instance. Then, from the type, you have to get the PropertyInfo
(through the GetProperty method on the Type) for the SomeProperty property.

Once you have that, you can get the value through the GetValue method on
the PropertyInfo that is returned from GetProperty.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Robert Towne" <no_spam@_nospam.com> wrote in message
news:uc****************@TK2MSFTNGP05.phx.gbl...
If I have a string representation of a property such as
"MyObject.SomeProperty", how would I then create a "MyObject" object and
invoke "SomeProperty". Any assistance would be appreciated. Thanks.

Jun 23 '06 #2
> Look in the System.Reflection namespace.

Specifically, you have to load the Type instance for MyObject, and then
pass that to the static CreateInstance method on the Activator class to
create an instance. Then, from the type, you have to get the PropertyInfo
(through the GetProperty method on the Type) for the SomeProperty
property.

Once you have that, you can get the value through the GetValue method
on the PropertyInfo that is returned from GetProperty.

Hope this helps.


Ok. I took a quick look and I think I'm on track now. Thanks very much
(appreciated).
Jun 23 '06 #3
"Robert Towne" <no_spam@_nospam.com> wrote in message
news:uc****************@TK2MSFTNGP05.phx.gbl...
If I have a string representation of a property such as
"MyObject.SomeProperty", how would I then create a "MyObject" object and
invoke "SomeProperty". Any assistance would be appreciated. Thanks.


Something like:

string initializer = "It's Chinatown, Jake";
Type type = typeof(string);
object o = Activator.CreateInstance(type, new object[] {
initializer.ToCharArray() });
PropertyInfo info = type.GetProperty("Length");
Debug.WriteLine(info.GetValue(o, null));

///ark
Jun 23 '06 #4

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

Similar topics

6
by: Steve | last post by:
Hi All! I wonder if anyone could help me with the underlying logic of a problem I have. At the moment my working pages run like so ---------------------------------------------------------...
4
by: Steve | last post by:
> You can pass parameters in the URL for a POSTed form: > > ## page2 > <form method="post" action="page3.php?data=$urlencoded_data"> > <input type="text" name="address"> > <input...
5
by: Al Davis | last post by:
Note: I tried cross-posting this message to several newsgoups, including comp.lang.perl.misc, c.l.p.moderated, comp.infosystems.www.authoring.cgi, comp.lang.javascript and comp.lang.php. Nothing...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
6
by: Rob Bolton | last post by:
Hi there, If a program running under the interactive logon session (say Susan), needs to impersonate Bob (via "LogonUser()"), but Bob needs to access the network as Susan (i.e., his local...
15
by: David | last post by:
Hey, I'm a fairly new person at ASP, and MS technologies kinda in general. I've been a big fan of PHP/MySQL, and unix environments for awhile. I used to do VC++ and VB with VS6, and have for...
2
by: daz_oldham | last post by:
Hi I was wondering if you could help me - I am looking at creating a C# Project to be used with my ASP.NET solition (VS2005). What I would like to do (I believe) is really simple. I have an...
3
by: Shal | last post by:
Hi all, I have a table with 300,000 records in it and I want to run an update that has a few clauses in the WHERE command and just changes one boolean value on a subset of records. What I am...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.