samedi 20 octobre 2018

Reflection: Get/Set Value of Property's PropertyType's Property

So I'm still automating my reading data into UI (working with Unity), and the situation is as such: On GameObject I have a script, in which I store variables/properties with their neat sorted attributes. I read them via reflection, for example:

public void insertAllFromGameObject(GameObject target, List<Type> types)
{
    var components  = target.GetComponents<Component>();
    foreach (var component in components)
    {
        if(types.Contains(component.GetType()))
        {
            var properties = component.GetType().GetProperties().Where(t => t.GetCustomAttributes<ListableAttribute>().Count() > 0);
            foreach(var p in properties)
            {
                Debug.Log("VALUE: "+p.GetValue(component, null));

This works. Now, let's say I have a property where I want to peak in it's class, list it's properties instead with values as set in this specific property, and potentially modify them one by one for this property.

I got as far as listing, but can't figure out what to pass as argument into GetValue. Example:

public void insertAllFromVariable(GameObject target, List<Type> types, string propertyName)
{
    var components  = target.GetComponents<Component>();
    foreach (var component in components)
    {
        if(types.Contains(component.GetType()))
        {
            var property = component.GetType().GetProperty(propertyName);

            var propertysProperties = property.PropertyType.GetProperties().Where(t => t.GetCustomAttributes<ListableAttribute>().Count() > 0);
            foreach(var p in propertysProperties)
            {
                Debug.Log("VALUE: "+p.GetValue(component, null));

This last line is a problem because of course I'm not looking for it in a "component" - but what should I pass in it so that it reflects my variable's values?





Aucun commentaire:

Enregistrer un commentaire