Exemplo n.º 1
0
        private async void InitPulginAsync()
        {
            await Task.Run(() =>
            {
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "plugin\\");
                if (!Directory.Exists(path) || Directory.GetFiles(path).Length < 1)
                {
                    return;
                }

                foreach (var item in Directory.GetFiles(path))
                {
                    if (Path.GetExtension(item) != ".dll" && Path.GetExtension(item) != ".exe")
                    {
                        continue;
                    }

                    var tp = Assembly.LoadFrom(item).GetTypes().FirstOrDefault(o => o.Name == "Rule");
                    if (tp == null)
                    {
                        continue;
                    }
                    var obj = (object)null;

                    Dispatcher.Invoke(() =>
                    {
                        obj = Activator.CreateInstance(tp);
                    });


                    // set SendMsg value
                    tp.GetProperty("SendMsg")?.SetValue(obj, new Action <string, string>((to, msg) =>
                    {
                        var fromNickName = WXService.Instance.Me.ShowName;
                        var toNickName   = _wxSerivice.GetNickName(to, msg);

                        PrintLin($"[{fromNickName}] - [{toNickName}] - {DateTime.Now}");
                        PrintLin($"Msg: {msg}\r\n");

                        _wxSerivice.SendMsgAsync(msg, _wxSerivice.Me.UserName, to, 1);
                    }));

                    if (_arg != "plugin")
                    {
                        tp.GetProperty("Me")?.SetValue(obj, new Tuple <string, string>(_wxSerivice.Me.NickName, _wxSerivice.Me.UserName));
                    }

                    SetRuleListBox(obj);
                }
            });
        }