Example #1
0
        private bool BindMappings()
        {
            List <Type>           selectedInjectors = _rootInjection.GetCheckedNodes <Type>();
            Func <BindItem, bool> predicate         = x => x.Method != null && x.Method.Body != null;
            List <BindItem>       selectedMethods   = _rootTarget.GetCheckedNodes <BindItem>(predicate);

            if (_mapping == null)
            {
                _mapping = new List <InjectionMapping>();
            }

            if (selectedInjectors.Count == 0 && _mapping.Count == 0)
            {
                MessageBox.Show(@"No code injectors selected!");
                return(false);
            }

            bool injectAllMethods = false;

            if (selectedMethods.Count == 0 && _mapping.Count == 0)
            {
                DialogResult result =
                    MessageBox.Show(
                        @"You have not selected any methods in the Target Assembly that need to be injected!",
                        Application.ProductName, MessageBoxButtons.OK);

                return(false);
            }

            if (_mapping.Count > 0)
            {
                DialogResult result =
                    MessageBox.Show(
                        @"You have already defined a mapping, do you want to add this to the mapping or override? Click Yes if you want to add, No if you want to override & Cancel otherwise",
                        Application.ProductName, MessageBoxButtons.YesNoCancel);

                if (result == System.Windows.Forms.DialogResult.No)
                {
                    _mapping = new List <InjectionMapping>();
                    lstOutput.Items.Clear();
                }
                else if (result == System.Windows.Forms.DialogResult.Cancel)
                {
                    return(false);
                }
            }

            btnRemove.Enabled = true;

            if (injectAllMethods)
            {
                selectedMethods = _rootTarget.GetNodes <BindItem>(predicate);
            }

            for (int methods = 0; methods < selectedMethods.Count; methods++)
            {
                for (int injector = 0; injector < selectedInjectors.Count; injector++)
                {
                    var newMapping = new InjectionMapping(selectedMethods[methods].Assembly,
                                                          selectedMethods[methods].Method,
                                                          selectedInjectors[injector]);

                    //selectedInjectors[injector]

                    if (_mapping.Count(x => x.GetHashCode() == newMapping.GetHashCode()) == 0)
                    {
                        _mapping.Add(newMapping);
                    }
                }
            }

            grdCombination.DataSource = null;
            grdCombination.DataSource = _mapping;
            grdCombination.Refresh();

            return(true);
        }