Ejemplo n.º 1
0
        public static MethodInfo Find(string name)
        {
            Assembly assembly = Assembly.GetEntryAssembly();

            foreach (Type item in assembly.GetExportedTypes())
            {
                if (item.GetCustomAttribute <ConsoleEntryPointAttribute>() == null)
                {
                    continue;
                }

                if (AliasAttribute.MatchName(item, name))
                {
                    MethodInfo result = item.GetMethod("Run", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public ConsoleWindow()
        {
            InitializeComponent();
            isProgramEnded  = 0;
            isWindowClosing = 0;

            resources        = new ResourceDictionary();
            resources.Source = new Uri("ConsoleResources.xaml", UriKind.Relative);

            listener = new TraceListener(this, listBoxLogs);
            Trace.Listeners.Add(listener);

            string classname = App.CommandLineArgs["class"];

            programEntry = ConsoleEntryPointAttribute.Find(classname);
            if (programEntry == null)
            {
                Close();
                return;
            }

            #region Command-Line에 입력한 인자들을 Console Program Parameter들에 맞게 변환합니다.
            ParameterInfo[] parameters = programEntry.GetParameters();
            object[]        args       = new object[parameters.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                foreach (KeyValuePair <string, string> item in App.CommandLineArgs)
                {
                    if (AliasAttribute.MatchName(parameters[i], item.Key))
                    {
                        try
                        {
                            args[i] = Convert.ChangeType(item.Value, parameters[i].ParameterType);
                        }
                        catch (FormatException) { args[i] = null; }
                        catch (InvalidCastException) { args[i] = null; }

                        break;
                    }
                }
            }
            #endregion

            programThread = new Thread(() =>
            {
                try
                {
                    programEntry.Invoke(null, args);
                    Interlocked.Exchange(ref isProgramEnded, 1);
                    if (isWindowClosing == 0)
                    {
                        this.Dispatcher.Invoke(new Action(() => { OnProgramEnded(); }));
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            });
            programThread.Start();
        }