- Home /
How to get a List of all Variables in a Class
So I defined a data-holder class that contains a big number of primitive variables. For simplicity let's say the class looks something like this:
public class Properties {
bool aBool;
float someFloat;
int anInteger;
}
I want to be able to attach any event to a set of conditions. As sourcecode this would look something like this:
public void CheckConditions(Properties properties) {
if (properties.aBool == true) {
//Run any Event
}
}
I don't want to define those Conditions inside my source code for several reasons: - The Conditions should be editable in a custom Editor window, without the need to touch any sourcecode. - Sets of Conditions should be saved into and loaded from files.
So I started creating condition-classes for every variable inside the "Properties" class which became kind of confusing pretty fast.
Now I'm trying to figure a generic way to read out a list of all the variable names and their types defined in the dataholder class inside my sourcecode so i can define something like "Every number value can be checked for an input value being bigger, smaller, equal or unequal" and show these options in my UI. Later I want to be able to create condition-sets for pre-defined events in my Script without the need of defining a new variable in 5 different locations everytime a new one is created.
Shortly, I'm looking for a function that outputs all the variables inside the data-holder class as string-array for example.
And I need another function that gets the value inside the variable that matches the given (string) variable name.
I imagine something like this:
public struct Variable {
string name;
Type type;
}
public class Properties {
public Variable getVairables() {
// ... ?
}
public T getValue<T>(string varName) {
// ... ?
}
}
I wonder if Serializing is the right direction here, I saw some functions that seam to deal with - let me call it "variable variables" - but I'm not sure about that though.
Any suggestions on how to achive this are very welcome! If this question has been answered allready please redirect me to any sources.
Thank you! And sorry for the lack of english skills ;)
You'd have to use C# Reflection. I'm not sure on the details exactly, but that would be a good place to start.
just a note, reflection is in no way related to the language
How is reflection not related to the language?
This is pretty much exactly what the question is asking for, and someone else has also commented below:
$$anonymous$$aybe this could be guiding you : https://unitygem.wordpress.com/json-serializer-attributes-reflection/
Go down to the reflection section. It is using attributes but the principle remains the same:
Type type = typeof($$anonymous$$yClass);
FieldInfo [] infos = type.GetFields();
foreach(FieldInfo fi in infos){ }
Answer by Dray · Nov 03, 2015 at 08:55 PM
Allright, thanks for your answers! In the end, the perfect conclusion for me is using the Type class, which serves functions for exactly what I want to achieve.
This function gives me all the Properties according to the Type that I pass to it using "myObject.GetType()":
public static Variable[] getProperties(Type type) {
var propertyValues = type.GetProperties ();
var result = new Variable[propertyValues.Length];
for (int i = 0; i < propertyValues.Length; i++) {
result[i].name = propertyValues[i].Name;
result[i].type = propertyValues[i].GetType();
}
return result;
}
And I use these Functions to read or write a values:
public object getValue(string name) {
return this.GetType().GetProperty(name).GetValue(this,null);
}
public void setValue(string name, object value) {
this.GetType().GetProperty(name).SetValue(this,value,null);
}
In my case I need the Objects Properties. You can also read Fields using "getFields()" instead and I read about some different ways to get public / private / etc. values but I closed the Tab allready and can't find the resource anymore. Sorry for that. Still I think it's good to know that they are there.
For those who are interested in this, here is a full class that provides functions to solve this Problem:
using System;
public class PropertyReader {
//simple struct to store the type and name of variables
public struct Variable {
public string name;
public Type type;
}
//for instances of classes that inherit PropertyReader
private Variable[] _fields_cache;
private Variable[] _props_cache;
public Variable[] getFields() {
if (_fields_cache == null)
_fields_cache = getFields (this.GetType ());
return _fields_cache;
}
public Variable[] getProperties() {
if (_props_cache == null)
_props_cache = getProperties (this.GetType ());
return _props_cache;
}
//getters and setters for instance values that inherit PropertyReader
public object getValue(string name) {
return this.GetType().GetProperty(name).GetValue(this,null);
}
public void setValue(string name, object value) {
this.GetType().GetProperty(name).SetValue(this,value,null);
}
//static functions that return all values of a given type
public static Variable[] getFields(Type type) {
var fieldValues = type.GetFields ();
var result = new Variable[fieldValues.Length];
for (int i = 0; i < fieldValues.Length; i++) {
result[i].name = fieldValues[i].Name;
result[i].type = fieldValues[i].GetType();
}
return result;
}
public static Variable[] getProperties(Type type) {
var propertyValues = type.GetProperties ();
var result = new Variable[propertyValues.Length];
for (int i = 0; i < propertyValues.Length; i++) {
result[i].name = propertyValues[i].Name;
result[i].type = propertyValues[i].GetType();
}
return result;
}
}
From the outside I call it like this:
PropertyReader.getProperties(typeof(AnyClass));
Or I inherit PropertyReader into another Class like this
public class MyProperties : PropertyReader {
bool aBool;
float someFloat
//...
}
Surely still needs to be optimized but that's the direction I took. Thanks for your help guys!
@Dray I have no idea how to use this. I'm trying to get variables from a script and I want to check which ones are floats, bools, and other. Please help
System.Type is a class. You can take any object in c# and call it's method GetType() that returns it's type. Here's $$anonymous$$SDN for it: System.Type. You can use its GetFields() method to get all fields (which you call variables) of your type. Then, you can check of what type those fields are, what is it's name, attributes, etc.
Don't forget to add the BindingFlags field in GetProperty() for parameters that are private, internal or protected! You will need to include BindingFlags.NonPublic eg. this.GetType().GetProperty("name", BindingFlags.NonPublic).SetValue(this, value);