Example #1
0
    static private object ProcUpdateComponent_Generic(MonoBehaviour pTargetMono, MemberInfo pMember, GetComponentInChildrenAttribute pGetComponentAttribute, System.Type pTypeField)
    {
        object pComponent = null;

        System.Type pTypeField_Generic = pTypeField.GetGenericTypeDefinition();
        Type[]      arrArgumentsType   = pTypeField.GetGenericArguments();

        if (pTypeField_Generic == typeof(List <>))
        {
            // List의 경우 GetComponentsInChildren 을 통해 Array를 얻은 뒤
            pComponent = pGetComponentAttribute.GetComponent(pTargetMono, arrArgumentsType[0]);
            // List 생성자에 Array를 집어넣는다.
            pMember.SetValue_Extension(pTargetMono, System.Activator.CreateInstance(pTypeField, pComponent));
        }
        else if (pTypeField_Generic == typeof(Dictionary <,>))
        {
            pComponent = ProcUpdateComponent_Dictionary(pTargetMono, pMember, pTypeField, pGetComponentAttribute, arrArgumentsType[0], arrArgumentsType[1]);
        }

        return(pComponent);
    }
Example #2
0
    static private object ProcUpdateComponent_Dictionary(MonoBehaviour pTargetMono, MemberInfo pMember, System.Type pTypeField, GetComponentInChildrenAttribute pAttributeInChildren, Type pType_DictionaryKey, Type pType_DictionaryValue)
    {
        object pComponent   = pAttributeInChildren.GetComponent(pTargetMono, pType_DictionaryValue);
        Array  arrComponent = pComponent as Array;

        if (arrComponent.Length == 0)
        {
            return(null);
        }

        var Method_Add = pTypeField.GetMethod("Add", new[] {
            pType_DictionaryKey, pType_DictionaryValue
        });

        // Reflection의 메소드는 Instance에서만 호출할수 있다.
        var  pInstanceDictionary       = System.Activator.CreateInstance(pTypeField);
        bool bIsDerived_DictionaryItem = false;

        Type[] arrInterfaces = pType_DictionaryValue.GetInterfaces();
        string strTypeName   = typeof(IDictionaryItem <>).Name;

        for (int i = 0; i < arrInterfaces.Length; i++)
        {
            if (arrInterfaces[i].Name.Equals(strTypeName))
            {
                bIsDerived_DictionaryItem = true;
                break;
            }
        }

        if (pType_DictionaryKey == typeof(string))
        {
            for (int k = 0; k < arrComponent.Length; k++)
            {
                Component pComponentChild = arrComponent.GetValue(k) as Component;
                Method_Add.Invoke(pInstanceDictionary, new object[] {
                    pComponentChild.name,
                    pComponentChild
                });
            }
        }
        else
        {
            if (bIsDerived_DictionaryItem)
            {
                var pMethod_GetKey = pType_DictionaryValue.GetMethod("IDictionaryItem_GetKey");
                for (int k = 0; k < arrComponent.Length; k++)
                {
                    Component pComponentChild = arrComponent.GetValue(k) as Component;
                    Method_Add.Invoke(pInstanceDictionary, new object[] {
                        pMethod_GetKey.Invoke(pComponentChild, null),
                        pComponentChild
                    });
                }
            }
            else
            {
                if (pType_DictionaryKey.IsEnum)
                {
                    for (int k = 0; k < arrComponent.Length; k++)
                    {
                        Component pComponentChild = arrComponent.GetValue(k) as Component;
                        try
                        {
                            var pEnum = System.Enum.Parse(pType_DictionaryKey, pComponentChild.name, true);
                            Method_Add.Invoke(pInstanceDictionary, new object[] {
                                pEnum,
                                pComponentChild
                            });
                        }
                        catch { }
                    }
                }
            }

            // Debug.LogError("GetComponentAttribute Error Dictionary Key 타입은 string, enum만 됩니다. Key Type : " + pType_DictionaryKey.Name);
            // return null;
        }

        pMember.SetValue_Extension(pTargetMono, pInstanceDictionary);
        return(pComponent);
    }