Is this possible? 'Dynamic' Code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a class called myConfigurationClass with 3 private members, and 3 public gets (so, 3 read-only properties). So, for instance

private members: _A, _B, _
public gets A, B,

Oh, and they are all strings. In particular, they are Connection strings for SQL Server

In my web config, I have an appSetting, let's call it "CurrentConnectionString", and its value is either "A", "B", or "C

How can you write code so that it reads the value of "CurrentConnectionString" and then executes the proper get

The typical way would be something like

Step 1) create a string to hold the value of the key

Step 2) using either case or if-else if-else, loop through and see what the value is, and do other stuff accordingl

Thus

System.Configuration.AppSettingsReader AppSettingsReader = new System.Configuration.AppSettingsReader()
string ConnectionStringType = (string)AppSettingsReader.GetValue("CurrentConnectionString", typeof(string))
string ConnectionString = ""
if (ConnectionString == "A"

ConnectionString = myConfigurationClass.A

else if (ConnectionString == "B"

ConnectionString = myConfigurationClass.B

els

ConnectionString = myConfigurationClass.



Is there a way that I could dump the if - else stuff and write something like

string ConnectionString = myConfigurationClass.[ConnectionStringType]; //which doesn't wor

where it 'dynamically' writes the code that follows "myConfigurationClass.

TI

jdn
 
Why not have one property that may return one of 3 values instead of 3
properties and needing to find out which you want?


jdn said:
I have a class called myConfigurationClass with 3 private members, and 3
public gets (so, 3 read-only properties). So, for instance:
private members: _A, _B, _C
public gets A, B, C

Oh, and they are all strings. In particular, they are Connection strings for SQL Server.

In my web config, I have an appSetting, let's call it
"CurrentConnectionString", and its value is either "A", "B", or "C"
How can you write code so that it reads the value of
"CurrentConnectionString" and then executes the proper get?
The typical way would be something like:

Step 1) create a string to hold the value of the key.

Step 2) using either case or if-else if-else, loop through and see what
the value is, and do other stuff accordingly
Thus:

System.Configuration.AppSettingsReader AppSettingsReader = new System.Configuration.AppSettingsReader();
string ConnectionStringType = (string)AppSettingsReader.GetValue("CurrentConnectionString",
typeof(string));
string ConnectionString = "";
if (ConnectionString == "A")
{
ConnectionString = myConfigurationClass.A;
}
else if (ConnectionString == "B")
{
ConnectionString = myConfigurationClass.B;
}
else
{
ConnectionString = myConfigurationClass.C
}



Is there a way that I could dump the if - else stuff and write something like:

string ConnectionString = myConfigurationClass.[ConnectionStringType]; //which doesn't work

where it 'dynamically' writes the code that follows "myConfigurationClass."

TIA

jdn
 
No. I don't think it is.

jdn said:
Well, there are different scenarios where what I am asking about would be
important (though there certainly is more than one way to skin a cat, as you
point out), so I'd like to know if it is possible.
Thanks.


----- Scott M. wrote: -----

Why not have one property that may return one of 3 values instead of 3
properties and needing to find out which you want?


jdn said:
I have a class called myConfigurationClass with 3 private members,
and 3
public gets (so, 3 read-only properties). So, for instance: strings
for SQL Server. what
the value is, and do other stuff accordingly
System.Configuration.AppSettingsReader();
string ConnectionStringType = (string)AppSettingsReader.GetValue("CurrentConnectionString",
typeof(string));
string ConnectionString = "";
if (ConnectionString == "A")
{
ConnectionString = myConfigurationClass.A;
}
else if (ConnectionString == "B")
{
ConnectionString = myConfigurationClass.B;
}
else
{
ConnectionString = myConfigurationClass.C
}
something
myConfigurationClass.[ConnectionStringType];
//which doesn't work
 
Look at the classes in System.Reflection. You can dynamically examine a
type, look at it's properties, etc, and invoke them given an instance of the
type. However, this is a lot more work then the kind of code you were
hoping, so you should consider if that's worth it or if this is absolutely
necessary, or if there are better ways to solve this problem.

One way is to interpret the value from web.config into an enumeration type -
have a central place where that is always done. Then your property could get
an enum as a parameter and act accordingly. This way you wouldn't have
strings all over the place that could potentially have a spelling error or
something that would cause incorrect behavior.

jdn said:
Well, there are different scenarios where what I am asking about would be
important (though there certainly is more than one way to skin a cat, as you
point out), so I'd like to know if it is possible.
Thanks.


----- Scott M. wrote: -----

Why not have one property that may return one of 3 values instead of 3
properties and needing to find out which you want?


jdn said:
I have a class called myConfigurationClass with 3 private members,
and 3
public gets (so, 3 read-only properties). So, for instance: strings
for SQL Server. what
the value is, and do other stuff accordingly
System.Configuration.AppSettingsReader();
string ConnectionStringType = (string)AppSettingsReader.GetValue("CurrentConnectionString",
typeof(string));
string ConnectionString = "";
if (ConnectionString == "A")
{
ConnectionString = myConfigurationClass.A;
}
else if (ConnectionString == "B")
{
ConnectionString = myConfigurationClass.B;
}
else
{
ConnectionString = myConfigurationClass.C
}
something
myConfigurationClass.[ConnectionStringType];
//which doesn't work
 
----- Marina wrote: -----

Look at the classes in System.Reflection. You can dynamically examine a
type, look at it's properties, etc, and invoke them given an instance of the
type. However, this is a lot more work then the kind of code you were
hoping, so you should consider if that's worth it or if this is absolutely
necessary, or if there are better ways to solve this problem.

One way is to interpret the value from web.config into an enumeration type -
have a central place where that is always done. Then your property could get
an enum as a parameter and act accordingly. This way you wouldn't have
strings all over the place that could potentially have a spelling error or
something that would cause incorrect behavior.



Thanks.

jdn
 
Back
Top