Accesing property name at design times

S

sracherla

Hello Guys,

Please look at the code sample below.

Class Customer
{
private string _ID;
private string _Name;

public string ID
{
get { return _ID; }
set { _ID = value; }
}

public string Name
{
get { return _Name; }
set { _ID = Name; }
}
}


void SomeFunction(Customer, PropertyName as String)
{
does something
}

============
In UI (Design Time): A different application that using the above
assembly

psv main()
{
Customer myC = new Customer();
myC.ID = "1";
myC.Name = "John Doe";

SomeFunction(Customer, "ID");

// Instead of the above statement I want something like
SomeFunction(Customer, Customer.Properties.ID);
}

What are the strategies to make this possible?

I know of one using an inner class. It is outlined below. In class
customer add the following:
public class Properties
{
public const string ID = "ID";
public const string Name = "Name";
}

Thanks
 
S

sloan

Here is snipplet.

object can be anything.

This code will take 1 object, instantiate another object, and if the
property names are the same, it will set the value of the second object to
objects one value.
Notice there is no case switching on type.

It assumes if the property name match, the datatype match.


object1 = new SomeObject();

destinationObject = new SomeObject();

PropertyInfo[] props =
object1.GetType().GetProperties();

//the next loop is basically a cloning mechanism.
foreach (PropertyInfo prop in props )
{
if (prop.CanWrite) // only update properties which can be written to
{

PropertyInfo indivReportProperty =
destinationObject.GetType().GetProperty(prop.Name);
if (null != indivReportProperty) //handle the case where the objects
are not perfect copies of one another
{
indivReportProperty.SetValue (destinationObject ,
(prop.GetValue(object1, null)) , null);
}

}
}
 
S

sracherla

We cannot use an enumeration because it evaluates to a number. It
should evaluate to a string that represents the property name.
 
S

sracherla

Your example is a nice one. But it does not address the issue here at
hand.

I want to be table to access the property names ( not values) at DESIGN
time.

I humbly suggest you reread my original post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top