I thought I could use Reflection to dynamically set properties within a class library. I know this is a bit strange, you'll have to trust I have a reason for this 
Say I have two strings:
Within that same class, I want a method that would take two strings:
An external program using this class library could:
Yes, I know I could create a public property that would get/set the private "_field1", and then I could simply access it directly. My actual need is more complex, and this is the simplest example I could think of that would still have the core requirement I seek, namely:
How can a class, get/set the value of a string object using a string VALUE equal to the name of the corresponding string object?
How can I make _field1 = "Hello world!", using "x" when "x" = "_field1"?
The actual problem:
I'm writing a class that will read/write/parse a particular fixed-width record format. The record contains a massive number of fields. Each field has a specific size.
Users of the class library will be able to set the value of all relevant fields and write the record(s).
If you've worked with fixed-width files before, you know that all fields, including empty fields, must be padded-out with spaces.
Each field will be a property (private/public strings) in the class. What I'm seeking is a more efficient way of initializing all the strings/fields to be fully padded out with spaces according to the size they need to be.
My thought was to create a Dictionary with each field/string name as a key and the field size as an integer value.
In the class constructor, I could do a foreach loop, getting the name of the string and it's size, and use "PadRight()" to fill that string with spaces.
The problem is, the "name of the string" doesn't equal the actual string. So I'd hoped that Reflection would allow me to say:
"Go get the string object named "whatever" and fill it with spaces".
Sorry for the verbosity; thanks for reading this far.
Thomas D. Greer
Say I have two strings:
Code:
private string _field1;
private string _field2;
Within that same class, I want a method that would take two strings:
Code:
public void setValue(string K, string V)
{
// using Reflection, set variable "K" equal to value "V"
}
An external program using this class library could:
Code:
MyClass x = new MyClass();
x.setValue("_field1","Hello world!");
Yes, I know I could create a public property that would get/set the private "_field1", and then I could simply access it directly. My actual need is more complex, and this is the simplest example I could think of that would still have the core requirement I seek, namely:
How can a class, get/set the value of a string object using a string VALUE equal to the name of the corresponding string object?
How can I make _field1 = "Hello world!", using "x" when "x" = "_field1"?
The actual problem:
I'm writing a class that will read/write/parse a particular fixed-width record format. The record contains a massive number of fields. Each field has a specific size.
Users of the class library will be able to set the value of all relevant fields and write the record(s).
If you've worked with fixed-width files before, you know that all fields, including empty fields, must be padded-out with spaces.
Each field will be a property (private/public strings) in the class. What I'm seeking is a more efficient way of initializing all the strings/fields to be fully padded out with spaces according to the size they need to be.
My thought was to create a Dictionary with each field/string name as a key and the field size as an integer value.
In the class constructor, I could do a foreach loop, getting the name of the string and it's size, and use "PadRight()" to fill that string with spaces.
The problem is, the "name of the string" doesn't equal the actual string. So I'd hoped that Reflection would allow me to say:
"Go get the string object named "whatever" and fill it with spaces".
Sorry for the verbosity; thanks for reading this far.
Thomas D. Greer