简体   繁体   中英

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 rather within a property inside a component) - but what should I pass in it so that it reflects my variable's values?

Modified your code, sure I have no running code but concept should be clear to you:

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()))
                {
                    PropertyInfo property = component.GetType().GetProperty(propertyName);
                    object theRealObject = property.GetValue(component);

                    PropertyInfo[] propertysProperties = theRealObject.GetType().GetProperties().Where(t => t.GetCustomAttributes<ListableAttribute>().Count() > 0);
                    foreach (PropertyInfo p in propertysProperties)
                    {
                        Debug.Log("VALUE: " + p.GetValue(theRealObject));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM