static private object SetMember_OnList(IGetComponentAttribute pGetComponentAttribute, MonoBehaviour pTargetMono, object pMemberOwner, MemberInfo pMember, Type pTypeField, Type[] arrArgumentsType) { object pComponent = pGetComponentAttribute.GetComponent(pTargetMono, arrArgumentsType[0]); Array arrComponent = pComponent as Array; var Method_Add = pTypeField.GetMethod("Add"); var pInstanceList = System.Activator.CreateInstance(pTypeField); for (int i = 0; i < arrComponent.Length; i++) { Method_Add.Invoke(pInstanceList, new object[] { arrComponent.GetValue(i) }); } pMember.SetValue_Extension(pMemberOwner, pInstanceList); return(pComponent); }
// ==================================================================================================================== static private object SetMember_OnGeneric(IGetComponentAttribute pGetComponentAttribute, MonoBehaviour pTargetMono, object pMemberOwner, MemberInfo pMember, System.Type pTypeField) { object pComponent = null; System.Type pTypeField_Generic = pTypeField.GetGenericTypeDefinition(); Type[] arrArgumentsType = pTypeField.GetGenericArguments(); if (pTypeField_Generic == typeof(List <>)) { pComponent = SetMember_OnList(pGetComponentAttribute, pTargetMono, pMemberOwner, pMember, pTypeField, arrArgumentsType); } else if (pTypeField_Generic == typeof(Dictionary <,>)) { pComponent = SetMember_OnDictionary(pGetComponentAttribute, pTargetMono, pMemberOwner, pMember, pTypeField, arrArgumentsType[0], arrArgumentsType[1]); } return(pComponent); }
static private object SetMember_OnDictionary(IGetComponentAttribute pAttributeInChildren, MonoBehaviour pTargetMono, object pMemberOwner, MemberInfo pMember, System.Type pTypeField, Type pType_DictionaryKey, Type pType_DictionaryValue) { object pComponent = pAttributeInChildren.GetComponent(pTargetMono, pType_DictionaryValue); Array arrComponent = pComponent as Array; if (arrComponent == null || arrComponent.Length == 0) { return(null); } var Method_Add = pTypeField.GetMethod("Add", new[] { pType_DictionaryKey, pType_DictionaryValue }); var pInstanceDictionary = System.Activator.CreateInstance(pTypeField); bool bIsDrived_DictionaryItem = CheckIsDERIVED_DictionaryItem(pType_DictionaryValue.GetInterfaces(), typeof(IDictionaryItem <>).Name); if (pType_DictionaryKey == typeof(string)) { for (int i = 0; i < arrComponent.Length; i++) { UnityEngine.Object pComponentChild = arrComponent.GetValue(i) as UnityEngine.Object; try { Method_Add.Invoke(pInstanceDictionary, new object[] { pComponentChild.name, pComponentChild }); } catch { Debug.LogError(pComponentChild.name + " Get Compeont - Dictionary Add - Overlap Key MonoType : " + pTargetMono.GetType() + "/Member : " + pMember.Name, pTargetMono); } } } else if (bIsDrived_DictionaryItem) { var pMethod_GetKey = pType_DictionaryValue.GetMethod("IDictionaryItem_GetKey"); for (int i = 0; i < arrComponent.Length; i++) { UnityEngine.Object pComponentChild = arrComponent.GetValue(i) as UnityEngine.Object; Method_Add.Invoke(pInstanceDictionary, new object[] { pMethod_GetKey.Invoke(pComponentChild, null), pComponentChild }); } } else if (pType_DictionaryKey.IsEnum) { for (int i = 0; i < arrComponent.Length; i++) { try { UnityEngine.Object pComponentChild = arrComponent.GetValue(i) as UnityEngine.Object; var pEnum = System.Enum.Parse(pType_DictionaryKey, pComponentChild.name, true); Method_Add.Invoke(pInstanceDictionary, new object[] { pEnum, pComponentChild }); } catch { } } } pMember.SetValue_Extension(pMemberOwner, pInstanceDictionary); return(pComponent); }
static public void DoUpdateGetComponentAttribute(MonoBehaviour pTargetMono, object pMemberOwner, MemberInfo pMemberInfo) { object[] arrCustomAttributes = pMemberInfo.GetCustomAttributes(true); for (int i = 0; i < arrCustomAttributes.Length; i++) { IGetComponentAttribute pGetcomponentAttribute = arrCustomAttributes[i] as IGetComponentAttribute; if (pGetcomponentAttribute == null) { continue; } System.Type pTypeMember = pMemberInfo.MemberType(); object pComponent = null; if (pTypeMember.IsGenericType) { pComponent = SetMember_OnGeneric(pGetcomponentAttribute, pTargetMono, pMemberOwner, pMemberInfo, pTypeMember); } else if (pTypeMember.HasElementType) { pComponent = pGetcomponentAttribute.GetComponent(pTargetMono, pTypeMember.GetElementType()); } else { pComponent = pGetcomponentAttribute.GetComponent(pTargetMono, pTypeMember); } if (pComponent == null) { if (pGetcomponentAttribute.bIsPrint_OnNotFound_GetComponent) { GetComponentInChildrenAttribute pAttribute = pGetcomponentAttribute as GetComponentInChildrenAttribute; if (pAttribute != null && pAttribute.bSearch_By_ComponentName) { Debug.LogError(pTargetMono.name + string.Format(".{0}<{1}>({2}) Result == null", pGetcomponentAttribute.GetType().Name, pTypeMember, pAttribute.strComponentName), pTargetMono); } else { Debug.LogError(pTargetMono.name + string.Format(".{0}<{1}> Result == null", pGetcomponentAttribute.GetType().Name, pTypeMember), pTargetMono); } } continue; } if (pTypeMember.IsGenericType == false) { if (pTypeMember.HasElementType == false) { Array arrComponent = pComponent as Array; if (arrComponent != null && arrComponent.Length != 0) { pMemberInfo.SetValue_Extension(pMemberOwner, arrComponent.GetValue(0)); } } else { if (pTypeMember == typeof(GameObject)) { pMemberInfo.SetValue_Extension(pMemberOwner, ((Component)pComponent).gameObject); } else { pMemberInfo.SetValue_Extension(pMemberOwner, pComponent); } } } } }