コード例 #1
0
    public ReturnValue Invoke(params object[] arguments)
    {
        Type[] parameters = new Type[arguments.Length];
        for (int i = 0; i < parameters.Length; i++)
        {
            parameters[i] = arguments[i].GetType();
        }
        object[] results  = new object[m_SerializedObject.targetObjects.Length];
        object   instance = null;

        for (int i = 0; i < m_SerializedObject.targetObjects.Length; i++)
        {
            instance = m_SerializedObject.targetObjects[i];
            string[] members = m_MethodPath.Split('.');

            for (int memberIndex = 0; memberIndex < members.Length; memberIndex++)
            {
                string memberName   = members[memberIndex];
                Type   instanceType = instance.GetType();

                if (string.CompareOrdinal("Array", memberName) == 0)
                {
                    // Skip to the next index
                    memberIndex++;
                    // Array.data[0] // Example of what we are trying to parse
                    string arrayPath = members[memberIndex];
                    // grab our index
                    int arrayIndex = ReflectedMembers.GetArrayIndexFromPropertyPath(arrayPath);
                    // Cast our instance as a IList
                    IList asList = (IList)instance;
                    // Grab the element
                    instance = asList[arrayIndex];
                }
                else if (memberIndex == members.Length - 1)
                {
                    m_MethodInfo = instanceType.GetMethod(memberName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic, null, parameters, null);
                }
                else
                {
                    FieldInfo fieldInfo = instanceType.GetField(memberName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                    instance = fieldInfo.GetValue(instance);
                }
            }

            if (m_MethodInfo.IsStatic)
            {
                // Invoke the method
                return(new ReturnValue(new object[] { m_MethodInfo.Invoke(instance, arguments) }));
            }
            // Invoke the method
            results[i] = m_MethodInfo.Invoke(instance, arguments);
        }
        return(new ReturnValue(results));
    }
コード例 #2
0
ファイル: ReflectedField.cs プロジェクト: sabresaurus/Weaver
    private void FindTarget(SerializedObject serializedObject, string propertyPath)
    {
        m_TargetInstance = null;

        for (int i = 0; i < serializedObject.targetObjects.Length; i++)
        {
            m_TargetInstance = serializedObject.targetObjects[i];
            string[] members = propertyPath.Split('.');

            for (int memberIndex = 0; memberIndex < members.Length; memberIndex++)
            {
                string memberName   = members[memberIndex];
                Type   instanceType = m_TargetInstance.GetType();

                if (string.CompareOrdinal("Array", memberName) == 0)
                {
                    // Skip to the next index
                    memberIndex++;
                    // Array.data[0] // Example of what we are trying to parse
                    string arrayPath = members[memberIndex];
                    // grab our index
                    int arrayIndex = ReflectedMembers.GetArrayIndexFromPropertyPath(arrayPath);
                    // Cast our instance as a IList
                    IList asList = (IList)m_TargetInstance;
                    // Grab the element
                    m_TargetInstance = asList[arrayIndex];
                }
                else if (memberIndex == members.Length - 1)
                {
                    m_FieldInfo = instanceType.GetField(memberName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                }
                else
                {
                    FieldInfo fieldInfo = instanceType.GetField(memberName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                    m_TargetInstance = fieldInfo.GetValue(m_TargetInstance);
                }
            }

            if (m_FieldInfo.IsStatic)
            {
                m_TargetInstance = null;
                // Only invoke static methods once.
                break;
            }
        }

        m_Value = (T)m_FieldInfo.GetValue(m_TargetInstance);
    }