- Home /
String as variable name, reflection or dictionary or other
What would be the fastest way, if we are talking about system performance here, of reading string as a variable name. So far I've read that there are options like reflection, dictionary or using an array. So I'm kinda confused right now, what are the differences between them and what method should i be using. And just so for the idea, what i would like to achieve:
//Class something
public string a="box";
public int box = 10;
So usual way of accessing variable box would be
something.box;
But instead, how would i go if I wanted to achieve this type of method of accessing the variable box(syntax is wrong here of course, It's just for the idea)
something.a;
//And it would read it as something.box;
Hey YoungDeveloper,
I'm sorry, but I don't really understand your question. Would it be possible for you to either explain it a little more, or give us a reason why you'd want a variable with a variable name.
Gibson
Hey, Yea, I sure asked that in a quite not understandable way, sorry. So the main thing i wanted to understand here is, is there a way of executing a code from string. The reason i want to do that, is because i have a class of many, many items (each in its own class), amount of items will sure be over hundreds over time. So that would sure be a performance pain, if i'd check all classes, using if's, to get data about what kind of object is that (what have i picked up), because information about all items is stored in one script. So i was thinking creating (script) a universal way of getting data of what pick-able item have you picked up. Because if so, adding new items (like lets say potions) to game will be much more faster in all ways, because all what i will have to do create a new little class inside item class.
So if I had a
string a = "item.weeder_1.name";
How to execute it as piece of code ? If this is still not enough, i will post an example script, hehe.
Thanks, $$anonymous$$arl.
Answer by iwaldrop · Jul 01, 2013 at 06:22 PM
You could defiantly use a dictionary. Why you'd want to, however, is a question you should really consider.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class DictionaryMethods : MonoBehaviour
{
private Dictionary <string, Action> methods;
void Awake()
{
methods = new Dictionary <string, Action>();
methods.Add("MethodOne", MethodOne);
methods.Add("MethodTwo", MethodTwo);
}
void Start()
{
methods["MethodOne"]();
}
void MethodOne()
{
Debug.Log("MethodOne()");
}
void MethodTwo()
{
}
}
Answer by BlueRaja_2014 · Jul 01, 2013 at 06:41 PM
You can do this using reflection, but it will be significantly slower than resolving the property at compile-time. I agree with @iwaldrop that there is almost certainly a better way to do whatever it is you're trying to do.
But to answer your question, assuming instance
in the name of your class instance and myProperty
is the name of public property whose value you want, just do
instance.GetType().GetProperty("myProperty").GetValue(instance, null);
I have a string were I stored the name of a script and i want to define an instance of that script to access some of its attributes , how can I do that using reflection? I mean how can use the string which hold the script name to define an instance of it ? thanks in advance