I'm working on a deserialization class in .NET, I have to develop a method that provides to me with a variable name that is stored in a string.
I have a string such as:
string string_name = "this_is_going_to_be_var_name";
Now what can I do so that my code dynamically declares a variable named this_is_going_to_be_var_name?
So to clear things up: There will be a deserialization class that will declare variables of the same names as strings provided as input with their PARENT TYPES as per wish of the Higher Level Programmer/User.
For Example: In javascript/jQuery, when I fetch JSON by making a request, the interpreter declares variable(s)/array(s) of the same name and assigns values to them. If {"var_name":"var_value"} is a JSON string, the interpreter will create a variable named var_name and will assign "var_value" to it such as json_data_object.var_name.
No you can't. C# variables are all statically declared.
The best thing you can do is create a dictionary and use keys instead of variable names.
// Replace object with your own type
Dictionary<string, object> myDictionary = new Dictionary<string, object>();
myDictionary.Add("this_is_going_to_be_var_name", value_of_the_variable);
// ...
// This is equivalent to foo($this_is_going_to_be_var_name) in PHP
foo(myDictionary["this_is_going_to_be_var_name"]);
This isn't possible, variable names are defined at compile time, not run time.
One approach is to create a dictionary or hash table to map string names to objects to sort of achieve what you want.
Not sure what you meant by
my code dynamically declares a variable named
this_is_going_to_be_var_name?
but the .Net version of what explode
does in PHP is Split
:
string[] zz = "this_is_going_to_be_var_name".Split('_');
The only thing that I can think on the moment (I haven't tested it so I have no clue if it is possible), is to have a object of type dynamic and then try to set the fields at runtime using reflection and InvokeMember(), which I can give a chance that it will work since the there is no validation on objects of type dynamic.
UPDATE:
I have tested it with ExpendoObject and InvokeMember doesn't appear to work (at least not with the default binder, but I haven't tested it with DynamicObject and allthough I don't give it to much chances to work you might still try it, you can check out http://msdn.microsoft.com/en-us/library/ee461504.aspx on how to use DynamicObject.
Take a look on Dynamically adding properties to an ExpandoObject which essentially describes a method in which the dynamic object is being cast as a IDictionary and then you can add properties to it by using the standard dictionary access, while they are actually getting properties of the object.
I tested it in a sample project by having a dynamic object of type ExpendoObject and then I add another variable that referenced it using type IDictionary, and then I tried to set and get properties on both, as in the following example:
dynamic test = new ExpandoObject();
//reference the object as a dictionary
var asDictinary = test as IDictionary<string, Object>;
//Test by setting it as property and get as a dictionary
test.testObject = 123;
Console.Write("Testing it by getting the value as if it was a dictionary");
Console.WriteLine(asDictinary["testObject"]);
//Test by setting as dictionary and get as a property
//NOTE: the command line input should be "input", or it will fail with an error
Console.Write("Enter the varible name, ");
Console.Write("note that for the example to work it should the word 'input':");
string variableName = Console.ReadLine();
Console.Write("Enter the varible value, it should be an integer: ");
int variableValue = int.Parse(Console.ReadLine());
asDictinary.Add(variableName, variableValue);
Console.WriteLine(test.input);//Provided that the command line input was "input"
(Still however in your case since you will anyway not access the properties directly in code I don't see the need for it and you would probably be able to use a Dictionary directly, and I don't understand why you need them to be properties of the object, which is only needed if you want to reference them at compile time.
But maybe I am misunderstanding you and you are looking for a dynamic variable and not for a dynamic property [something that is available in PHP using the $$ syntax], if this is the case then note that in c# there are no variables at all as everything is encapsulated in a object).
You can also take a look in How can I dynamically add a field to a class in C# for more answers.