Dynamically add property values

T

tshad

I am trying to add values to a property in my class dynamically.

In my problem, I would have something like:

string value = "somevalue"
string name = "ColumnName"

or

string name = "ColumnType"

I have a class that has ColumnName and ColumnType as properties (there are
many more but this is just an illustration).

I don't want to create a big switch statement or a bunch of "if" statements
that in essence say:

if (name == "ColumnName")
tableColumnName.ColumnName = value
if (name == 'ColumnType")
tableColumnName.ColumnType = value

Is there a way using the column names in the name field to dynamically move
the data into the correct property? Something like:

tableColumnName.??? = value

where ??? would somehow use the "name" field

My class is:
**********************************************
using System;
using System.Collections.Generic;
using System.Text;

namespace RepositoryServiceApp.BusinessLayer
{
public class TableColumnName
{
private string mstrTableName;
private string mstrColumnName;
private string mstrColumnType;

public string ColumnType
{
get { return mstrColumnType; }
set { mstrColumnType = value; }
}

public string ColumnName
{
get { return mstrColumnName; }
set { mstrColumnName = value; }
}

public string TableName
{
get { return mstrTableName; }
set { mstrTableName = value; }
}

public new string ToString()
{
return TableName + " / " + ColumnName + " / " + ColumnType;
}
}
public class TableColumnNameCollection : List<TableColumnName> { }
}
*******************************************************

Thanks,

Tom
 
J

Jeff Johnson

tshad said:
I am trying to add values to a property in my class dynamically.

In my problem, I would have something like:

string value = "somevalue"
string name = "ColumnName"

or

string name = "ColumnType"

I have a class that has ColumnName and ColumnType as properties (there are
many more but this is just an illustration).

I don't want to create a big switch statement or a bunch of "if"
statements that in essence say:

if (name == "ColumnName")
tableColumnName.ColumnName = value
if (name == 'ColumnType")
tableColumnName.ColumnType = value

Is there a way using the column names in the name field to dynamically
move the data into the correct property? Something like:

tableColumnName.??? = value

where ??? would somehow use the "name" field


Not something that simple, but you can use this:

tableColumnName.GetType().InvokeMember(name, BindingFlags.Instance |
BindingFlags.Public | BindingFlags.SetProperty, null, tableColumnName,
new object[] { value });

Notice that you have to supply the variable name twice: once as the source
object and once as a parameter. It seems redundant, I know.
 
A

Anthony Jones

tshad said:
I am trying to add values to a property in my class dynamically.

In my problem, I would have something like:

string value = "somevalue"
string name = "ColumnName"

or

string name = "ColumnType"

I have a class that has ColumnName and ColumnType as properties (there are
many more but this is just an illustration).

I don't want to create a big switch statement or a bunch of "if"
statements that in essence say:

if (name == "ColumnName")
tableColumnName.ColumnName = value
if (name == 'ColumnType")
tableColumnName.ColumnType = value

Is there a way using the column names in the name field to dynamically
move the data into the correct property? Something like:

tableColumnName.??? = value

where ??? would somehow use the "name" field

My class is:
**********************************************
using System;
using System.Collections.Generic;
using System.Text;

namespace RepositoryServiceApp.BusinessLayer
{
public class TableColumnName
{
private string mstrTableName;
private string mstrColumnName;
private string mstrColumnType;

public string ColumnType
{
get { return mstrColumnType; }
set { mstrColumnType = value; }
}

public string ColumnName
{
get { return mstrColumnName; }
set { mstrColumnName = value; }
}

public string TableName
{
get { return mstrTableName; }
set { mstrTableName = value; }
}

public new string ToString()
{
return TableName + " / " + ColumnName + " / " + ColumnType;
}
}
public class TableColumnNameCollection : List<TableColumnName> { }
}
*******************************************************

Why would a class called TableColumnName have a property called ColumnName.
Surely you would have a TableColumn class that has a Name property?

Questions like these seem to be like buses; you can wait ages then two come
along at the same time. (See "Referencing object properties" earlier in
this group).

string value = "somevalue"
string name = "Name"



Type t = typeof(tableColumn);

t.GetProperty(name).SetValue(tableColumn, value, null);
 
J

Jeff Johnson

Type t = typeof(tableColumn);

t.GetProperty(name).SetValue(tableColumn, value, null);

Ah. Nice to know there's a shorter way than what I posted in my reply.
 
T

tshad

Anthony Jones said:
Why would a class called TableColumnName have a property called
ColumnName. Surely you would have a TableColumn class that has a Name
property?

Questions like these seem to be like buses; you can wait ages then two
come along at the same time. (See "Referencing object properties" earlier
in this group).

string value = "somevalue"
string name = "Name"



Type t = typeof(tableColumn);

t.GetProperty(name).SetValue(tableColumn, value, null);
That worked great.

What is the null in SetValue for? I could find the GetProperty on MSDN but
couldn't seem to find the GetProperty.SetValue there.

Thanks,

Tom
 
A

Anthony Jones

tshad said:
That worked great.

What is the null in SetValue for?

In .NET a property may have a number of parameters that index into the
property, the third parameter is an array which would contain the set of
arguments for such a property. C# only supports this behaviour on the
indexer of an object, it does not support this feature on named properties,
other languages such as VB do.
I could find the GetProperty on MSDN but couldn't seem to find the
GetProperty.SetValue there.

GetProperty returns PropertyInfo so did you look at the documentation of
PropertyInfo?
 

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