Exemplo n.º 1
0
        // Invoker.SetValue<T>() //
        #region [public] SetValue<T>(string fullname, T value, object[] index = null)
        public static T SetValue <T>(string fullname, T value, object[] index = null)
        {
            if (!string.IsNullOrEmpty(fullname) && fullname.IndexOf('.') > 0)
            {
                try
                {
                    string[] names = fullname.Split('.');
                    string   type  = string.Join(".", names, 0, names.Length - 1);
                    string   name  = names[names.Length - 1];

                    Type invokeType = null;
                    if (!Types.TryGetValue(type, out invokeType))
                    {
                        throw new Exception("Type '" + type + "' not exists in assemblie(s)");
                    }

                    Type targetType = invokeType;
                    while (invokeType != null)
                    {
                        PropertyInfo property = invokeType.GetProperty(name, AllBindingFlags);
                        if (property != null && property.PropertyType == typeof(T))
                        {
                            property.SetValue(null, value, index);
                            return((T)property.GetValue(null, index));
                        }

                        FieldInfo field = invokeType.GetField(name, AllBindingFlags);
                        if (field != null && field.FieldType == typeof(T))
                        {
                            field.SetValue(null, value);
                            return((T)field.GetValue(null));
                        }

                        invokeType = invokeType.BaseType;
                    }

                    throw new Exception("Field or property '" + name + "' not exists in '" + targetType.FullName + "'.");
                }
                catch (Exception E)
                {
                    if (string.IsNullOrEmpty(E.StackTrace))
                    {
                        ConsoleWindow.WriteLine(ConsoleColor.Red, "[InvokerException] " + E.Message);
                    }
                }
            }
            return(default(T));
        }
Exemplo n.º 2
0
        public static object Call(string name, object target, object[] arguments)
        {
            if (!string.IsNullOrEmpty(name) && target != null)
            {
                try
                {
                    Type   invokeType = target.GetType();
                    Type[] parameters = new Type[arguments.Length];
                    for (int i = 0; i < arguments.Length; i++)
                    {
                        if (arguments[i] != null)
                        {
                            parameters[i] = arguments[i].GetType();
                        }
                        else
                        {
                            parameters[i] = typeof(object);
                        }
                    }

                    MethodInfo invokeMethod = invokeType.GetMethod(name, AllBindingFlags, null, CallingConventions.Any, parameters, null);

                    if (invokeMethod == null)
                    {
                        foreach (var method in invokeType.GetMethods(AllBindingFlags))
                        {
                            if (method.Name == name && method.GetParameters().Length == parameters.Length)
                            {
                                invokeMethod = method;
                                break;
                            }
                        }
                    }

                    if (invokeMethod == null)
                    {
                        throw new Exception("Method '" + name + "' not exists in '" + invokeType.FullName + "'.");
                    }

                    return(invokeMethod.Invoke(target, arguments));
                }
                catch (Exception E)
                {
                    ConsoleWindow.WriteLine(ConsoleColor.Red, "[InvokerException] " + E.Message);
                }
            }
            return(null);
        }
Exemplo n.º 3
0
        // Console Update //

        #region [public] Update()
        public static void Update()
        {
            if (ConsoleWindow.IsValid)
            {
                // Output Console Input //
                if (OnInputText != null)
                {
                    ConsoleWindow.InputUpdate();
                }

                if (nextUpdate < UnityEngine.Time.time)
                {
                    ConsoleWindow.InputRedraw();
                }
            }
        }
Exemplo n.º 4
0
        private static void OnEnter()
        {
            ConsoleWindow.ClearLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("> " + InputString);

            inputHistory.Add(InputString);
            inputHistoryIndex = inputHistory.Count;

            string inputString = InputString;

            InputString     = string.Empty;
            inputCursorLeft = 0;

            InputRedraw();

            OnInputText?.Invoke(inputString);
        }
Exemplo n.º 5
0
 public static object Call(string name, object target)
 {
     if (!string.IsNullOrEmpty(name) && target != null)
     {
         try
         {
             Type       invokeType   = target.GetType();
             MethodInfo invokeMethod = invokeType.GetMethod(name, AllBindingFlags, null, new Type[0], null);
             if (invokeMethod == null)
             {
                 throw new Exception("Method '" + name + "' not exists in '" + invokeType.FullName + "'.");
             }
             return(invokeMethod.Invoke(target, new object[0]));
         }
         catch (Exception E)
         {
             ConsoleWindow.WriteLine(ConsoleColor.Red, "[InvokerException] " + E.Message);
         }
     }
     return(null);
 }
Exemplo n.º 6
0
        public static T SetValue <T>(object target, string name, T value, object[] index = null)
        {
            if (!string.IsNullOrEmpty(name) && target != null)
            {
                try
                {
                    Type targetType = target.GetType();
                    Type invokeType = targetType;
                    while (invokeType != null)
                    {
                        PropertyInfo property = invokeType.GetProperty(name, AllBindingFlags);
                        if (property != null && property.PropertyType == typeof(T))
                        {
                            property.SetValue(target, value, index);
                            return((T)property.GetValue(target, index));
                        }

                        FieldInfo field = invokeType.GetField(name, AllBindingFlags);
                        if (field != null && field.FieldType == typeof(T))
                        {
                            field.SetValue(target, value);
                            return((T)field.GetValue(target));
                        }

                        invokeType = invokeType.BaseType;
                    }

                    throw new Exception("Field or property '" + name + "' not exists in '" + targetType.FullName + "'.");
                }
                catch (Exception E)
                {
                    if (string.IsNullOrEmpty(E.StackTrace))
                    {
                        ConsoleWindow.WriteLine(ConsoleColor.Red, "[InvokerException] " + E.Message);
                    }
                }
            }
            return(default(T));
        }
Exemplo n.º 7
0
        public static object CallBase(string name, object target, object[] arguments)
        {
            if (!string.IsNullOrEmpty(name) && target != null)
            {
                try
                {
                    Type   targetType = target.GetType().BaseType;
                    Type[] parameters = new Type[arguments.Length];
                    for (int n = 0; n < arguments.Length; n++)
                    {
                        parameters[n] = arguments[n].GetType();
                    }

                    MethodInfo method = targetType.GetMethod(name, AllBindingFlags, null, CallingConventions.Any, parameters, null);

                    if (method == null)
                    {
                        foreach (var m in targetType.GetMethods(AllBindingFlags))
                        {
                            if (m.Name == name && m.GetParameters().Length == parameters.Length)
                            {
                                method = m;
                                break;
                            }
                        }
                    }

                    DynamicMethod dynamicMethod = null;
                    if (!DynamicMethods.TryGetValue(method, out dynamicMethod))
                    {
                        dynamicMethod = new DynamicMethod(name, typeof(object), new[] { targetType, typeof(object) }, targetType);

                        ParameterInfo[] parameterInfo = method.GetParameters();
                        int             i; Type[] paramTypes = new Type[parameterInfo.Length];

                        for (i = 0; i < paramTypes.Length; i++)
                        {
                            if (parameterInfo[i].ParameterType.IsByRef)
                            {
                                paramTypes[i] = parameterInfo[i].ParameterType.GetElementType();
                            }
                            else
                            {
                                paramTypes[i] = parameterInfo[i].ParameterType;
                            }
                        }

                        ILGenerator IL = dynamicMethod.GetILGenerator();
                        IL.Emit(OpCodes.Ldarg_0);

                        for (i = 0; i < paramTypes.Length; i++)
                        {
                            IL.Emit(OpCodes.Ldarg_1);
                            IL.Emit(OpCodes.Ldc_I4_S, i);
                            IL.Emit(OpCodes.Ldelem_Ref);

                            if (paramTypes[i].IsValueType)
                            {
                                IL.Emit(OpCodes.Unbox_Any, paramTypes[i]);
                            }
                            else if (paramTypes[i] != typeof(object))
                            {
                                IL.Emit(OpCodes.Castclass, paramTypes[i]);
                            }
                        }

                        IL.Emit(OpCodes.Call, method);

                        if (method.ReturnType == typeof(void))
                        {
                            IL.Emit(OpCodes.Ldnull);
                        }
                        else if (method.ReturnType.IsValueType)
                        {
                            IL.Emit(OpCodes.Box, method.ReturnType);
                        }

                        IL.Emit(OpCodes.Ret);

                        DynamicMethods[method] = dynamicMethod;
                    }

                    return(dynamicMethod.Invoke(null, new object[] { target, arguments }));
                }
                catch (Exception E)
                {
                    ConsoleWindow.WriteLine(ConsoleColor.Red, "[InvokerException] " + E.Message);
                }
            }
            return(null);
        }
Exemplo n.º 8
0
 /// <summary>
 ///
 /// </summary>
 private static void ConsoleThreadProcedure()
 {
     consoleWindow = new ConsoleWindow();
     application   = new Application();
     application.Run(consoleWindow);
 }