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!