Indirection Question

C

cmitroka

I'm writing a C# class and need to do something known to me in Cache (another
language) as indirection. Not sure if the functionality exists in C#, but
could really use some help here. Basically, pretend a method takes in two
string variables: field (and we'll say the value is Name) and fieldvalue (and
we'll say the value is Chris). This is within a public class having a field
of Name. I need that variable to now be set to Chris. Any ideas how I can
accompilsh this without a switch or if/else?
 
I

Ignacio Machin ( .NET/ C# MVP )

I'm writing a C# class and need to do something known to me in Cache (another
language) as indirection. Not sure if the functionality exists in C#, but
could really use some help here. Basically, pretend a method takes in two
string variables: field (and we'll say the value is Name) and fieldvalue (and
we'll say the value is Chris). This is within a public class having a field
of Name. I need that variable to now be set to Chris. Any ideas how I can
accompilsh this without a switch or if/else?

Hi,

Take a look at Reflection,
method(string propName, strnig Value){
this.GetType().InvokeMember(propName,
System.Reflection.BindingFlags.SetProperty, null, new object[]
{ propValue });
}
 

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

Similar Threads

Indirection Functionality 6
Addressing Class Properties 4
Serialization question 1
class properties 9
Inheritance 3
Get Strings 3
Get/Set vs Public Variables 27
property in a class 4

Top