473,467 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting class property value from string

Hi,

I am wondering if this can be done.
Well i have a class that take any object, and the class will get string from
text file which is value need to be retrieved from this object. For example
the property called Name of the class Proposal. what i need is my class to be
able to read the string "Proposal.Name" and get the value Name from the
object without knowing the object Proposal.
Is there a way for this to be done?
Aug 1 '06 #1
2 1691
Maybe you can use XML to represent your object text value. So, Using
XML serialization or XML API to parse XML string will be a easy job.

plz refer to System.Xml or System.Xml.Serialization namespace. Good
luck.

Sincerely,
simida

Coco 写道:
Hi,

I am wondering if this can be done.
Well i have a class that take any object, and the class will get string from
text file which is value need to be retrieved from this object. For example
the property called Name of the class Proposal. what i need is my class to be
able to read the string "Proposal.Name" and get the value Name from the
object without knowing the object Proposal.
Is there a way for this to be done?
Aug 1 '06 #2
Coco wrote:
I am wondering if this can be done.
Well i have a class that take any object, and the class will get string from
text file which is value need to be retrieved from this object. For example
the property called Name of the class Proposal. what i need is my class to be
able to read the string "Proposal.Name" and get the value Name from the
object without knowing the object Proposal.
Is there a way for this to be done?
I think that a little bit of reflection would do the trick. A complete
sample follows:

class Proposal
{
public string Name
{
get { return "Value of Proposal.Name"; }
}
}

class ReflectionReader
{
public object ReadPropertyValue(object o, string propertyName)
{
object result = null;

PropertyInfo propInfo =
o.GetType().GetProperty(propertyName);

if(propInfo != null)
{
result = propInfo.GetValue(o, null);
}

return result;
}
}

class Program
{
static void Main()
{
Console.WriteLine
(
new ReflectionReader().
ReadPropertyValue(new Proposal(), "Name")
);
}
}

Best regards!
Marcin
Aug 1 '06 #3

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

Similar topics

3
by: Pavils Jurjans | last post by:
Hallo, Is there some decent way how to get the object class name in a string format? Currently I use this: function getClassName(obj) { if (typeof obj != "object" || obj === null) return...
1
by: Michael Albanese | last post by:
I am building an ASP.Net web application that records employee incident data over several screens. I have built custom classes to hold this information as the user enters data. In order to persist...
10
by: Not Available | last post by:
On the host server: namespace JCart.Common public class JCartConfiguration : IConfigurationSectionHandler private static String dbConnectionString; public static String ConnectionString { get...
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
3
by: Bmack500 | last post by:
I have a sub, and a class. The sub is like this: Sub dothis() Dim aInfoListX As New infoWrapperClass aInfoListX.params = ReadConfig() Dim sqlCMD As New SqlCommand aInfoListX.sqlCmd = sqlCMD ...
2
by: RSH | last post by:
I have a situation where I am creating a class (oValue) that contains two properties "Name" and "Value". I am using a thrid party grid control which has a DropDownList Control. The...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
5
by: dgleeson3 | last post by:
Hello all I have used the LoginForm1 class in a Visual studio 2005 VB application. Its the standard Username, Pasword request for user input. I was hoping to use property procedures to get...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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 projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.