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 :wink:

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_.name = propertyValues*.Name;*_

result_.type = propertyValues*.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_.name = fieldValues*.Name;
result.type = fieldValues.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_.name = propertyValues*.Name;
result.type = propertyValues.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