Esempio n. 1
0
        private void Initialize(CommandAttribute attribute, MemberInfo info, MethodInfo method, FieldInfo field, PropertyInfo property)
        {
            this.declaringType = info.DeclaringType;
            this.attribute     = attribute;

            mainInfo = new _MemberInfo(method, field, property);
            BindingFlags flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;

            if (linkedType != null)
            {
                if (method != null)
                {
                    linkedInfo = new _MemberInfo(linkedType.GetMethod(method.Name, flags), null, null);
                }
                else if (field != null)
                {
                    linkedInfo = new _MemberInfo(null, linkedType.GetField(field.Name, flags), null);
                }
                else if (property != null)
                {
                    linkedInfo = new _MemberInfo(null, null, linkedType.GetProperty(property.Name, flags));
                }
            }

            if (method != null)
            {
                isStatic = method.IsStatic;
            }
            if (field != null)
            {
                isStatic = field.IsStatic;
            }
            if (property != null)
            {
                isStatic = property.GetAccessors(true)[0].IsStatic;
            }

            if (method != null)
            {
                //set parameters
                ParameterInfo[] ps = method.GetParameters();
                for (int i = 0; i < ps.Length; i++)
                {
                    parameters.Add(new ParamInfo(ps[i].Name, ps[i].ParameterType, ps[i].IsOptional, ps[i].DefaultValue));
                }
            }
            else if (property != null)
            {
                if (mainInfo.set != null)
                {
                    parameters.Add(new ParamInfo("value", property.PropertyType, false, null));
                }
            }
            else if (field != null)
            {
                parameters.Add(new ParamInfo("value", field.FieldType, false, null));
            }
        }
Esempio n. 2
0
        public object Invoke(object owner, bool useLinked, params object[] parameters)
        {
            bool isInMainMenuScene = GameManager.isInMainMenuScene;

            if (inGameOnly && isInMainMenuScene)
            {
                return("Command: '" + Name + "' is only available during gameplay!");
            }

            _MemberInfo info = !useLinked ? mainInfo : linkedInfo;

            if (info.method != null)
            {
                return(info.method.Invoke(owner, parameters));
            }
            else if (info.property != null)
            {
                if (parameters == null || parameters.Length == 0)
                {
                    //if no parameters were passed, then get
                    if (info.get != null)
                    {
                        return(info.get.Invoke(owner, parameters));
                    }
                }
                else if (parameters != null && parameters.Length == 1)
                {
                    //if 1 parameter was passed, then set
                    if (info.set != null)
                    {
                        info.property.SetValue(owner, parameters[0]);
                    }
                }
            }
            else if (info.field != null)
            {
                if (parameters == null || parameters.Length == 0)
                {
                    return(info.field.GetValue(owner));
                }
                else if (parameters != null && parameters.Length == 1)
                {
                    info.field.SetValue(owner, parameters[0]);
                }
            }
            return(null);
        }