Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#插件(反射)_C#_Plugins_Reflection - Fatal编程技术网

c#插件(反射)

c#插件(反射),c#,plugins,reflection,C#,Plugins,Reflection,我在创建带有反射的插件解决方案时遇到问题。当我点击菜单项时,我想在我的窗口中加载并显示另一个程序。我想我需要3个项目 单击菜单项时要加载另一个程序的客户端程序 我要加载的程序 插件类 代码客户端程序 private void nQueensToolStripMenuItem_Click(object sender, EventArgs e) { // Create an assembly object to load our classes string

我在创建带有反射的插件解决方案时遇到问题。当我点击菜单项时,我想在我的窗口中加载并显示另一个程序。我想我需要3个项目

  • 单击菜单项时要加载另一个程序的客户端程序
  • 我要加载的程序
  • 插件类
  • 代码客户端程序

    private void nQueensToolStripMenuItem_Click(object sender, EventArgs e)
        {
    
            // Create an assembly object to load our classes
            string path = Application.StartupPath + "\\NQueens.dll";
            Assembly ass = Assembly.LoadFile(path);
    
            Type objType = ass.GetType("NQueens.NQueen");
    
            // Create an instace of NQueens.NQueen
            var instance = Activator.CreateInstance(objType);
    
            // public static bool berekenQueens()
            objType.InvokeMember("berekenQueens",
                BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static,
                null, instance, null);
    
            // private static bool bordValidatie
            objType.InvokeMember("bordValidatie",
                BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static,
                null, instance, null);
        }
    
    阶级

    NQueens(单击菜单项时要加载的程序) Nquiens S.cs

    public class NQueen
    {
       public static bool berekenQueens(int Row, int N, bool[,] bord)
       {
           if (Row >= N) return true; 
    
           for (int Col = 0; Col < N; Col++)
           {
               //Q toevoegen
               bord[Row, Col] = true;
    
               //Q + Q volgende Row  controleren
               if (bordValidatie(Row, Col, bord, N) && berekenQueens(Row + 1, N, bord))
               {
                   return true;
               }
    
               //Q verwijderen indien niet door controle
               bord[Row, Col] = false;
    
           }
           return false;
       }
    
    
       private static bool bordValidatie(int currentRow, int currentCol, bool[,] currentBord, int N)
       {
           int colstep = 1;
           for (int i = currentRow - 1; i >= 0; i--)
           {
               //rechte lijn 
               if (currentBord[i, currentCol])
                   return false;
    
               //linker diagonaal
               if (currentCol - colstep >= 0)
               {
                   if (currentBord[i, currentCol - colstep])
                       return false;
               }
               //rechter diagonaal
               if (currentCol + colstep < N)
               {
                   if (currentBord[i, currentCol + colstep])
                       return false;
               }
               colstep += 1;
           }
           return true;
       }
    
    
    }
    
    public class-nquien
    {
    公共静态布尔贝雷肯皇后区(整数行,整数N,布尔[,]bord)
    {
    如果(行>=N)返回true;
    for(int Col=0;Col=0;i--)
    {
    //李丽君
    if(currentBord[i,currentCol])
    返回false;
    //链接器对角
    如果(currentCol-colstep>=0)
    {
    if(currentBord[i,currentCol-colstep])
    返回false;
    }
    //再诊断
    如果(当前列+列步
    主窗口(nqueens)

    公共部分类主窗口:窗口
    {
    {get{return Convert.ToInt32(txtN.Text);}set{txtN.Text=”“+value;}中的公共int
    私人布尔[,]spelbord;
    公共主窗口()
    {
    初始化组件();
    }
    私有无效按钮1\u单击(对象发送者,路由目标)
    {
    spelbord=新布尔[英寸,英寸];
    贝雷肯奎恩斯(0,在斯皮尔福德);
    可视化(在,斯皮尔福德);
    }
    私有虚空可视化(int N,bool[,]bord)
    {
    gridTekenen();
    对于(int行=0;行
    我一直在努力让这东西工作。我如何制作我的程序来显示NQueens程序?我的客户机程序如何从NQueen知道方法并使用它们


    编辑:我对nqueenstolstripmenuitem\u Click方法做了一些更改。现在是这样吗?现在如何在我的窗口中显示NQueens方法?

    据我所知,您将需要一个公共接口,它有助于与“扩展”进行通信。然后您将有三个项目:

    • 您的核心应用程序
    • 仅包含接口的库(即IPlugin)
    • 你的分机

    请看一看,以更好地了解插件的体系结构。

    一个更简单的模式是带有接口的经典“工厂模式”。我以前做过创建插件的工作,非常简单:

    1) 创建一个包含您将使用以“与插件对话”的所有属性和方法的接口。在一个基本类/程序集中定义它,它几乎不会改变

    2) 创建一个框架UI应用程序,该应用程序可能有一个插件接口的库存实现,也可能是默认菜单,这样您就可以轻松地调试并获得正确的接口

    3) 现在是动态加载。。。创建一个配置字符串,列出应加载的多个文件。很好,允许这是可配置的,而不是硬编码路径,同样出于安全考虑,永远不要在路径中加载程序集,您不知道谁可以通过文件权限中的后门将它们放在那里

    4) 启动时,使用程序集load from方法遍历每个文件,加载后使用反射查询接口,查找插件接口。存储已加载接口/插件的集合,并根据需要使用它们

    显然,当您有一个UI时,您将有一个方法来创建子菜单项和窗口,并将它们的父项适当地设置为框架应用程序菜单或客户端区域。然后,您将在界面上定义一些事件来处理诸如被单击的项目之类的事件。也可以定义
    public class NQueen
    {
       public static bool berekenQueens(int Row, int N, bool[,] bord)
       {
           if (Row >= N) return true; 
    
           for (int Col = 0; Col < N; Col++)
           {
               //Q toevoegen
               bord[Row, Col] = true;
    
               //Q + Q volgende Row  controleren
               if (bordValidatie(Row, Col, bord, N) && berekenQueens(Row + 1, N, bord))
               {
                   return true;
               }
    
               //Q verwijderen indien niet door controle
               bord[Row, Col] = false;
    
           }
           return false;
       }
    
    
       private static bool bordValidatie(int currentRow, int currentCol, bool[,] currentBord, int N)
       {
           int colstep = 1;
           for (int i = currentRow - 1; i >= 0; i--)
           {
               //rechte lijn 
               if (currentBord[i, currentCol])
                   return false;
    
               //linker diagonaal
               if (currentCol - colstep >= 0)
               {
                   if (currentBord[i, currentCol - colstep])
                       return false;
               }
               //rechter diagonaal
               if (currentCol + colstep < N)
               {
                   if (currentBord[i, currentCol + colstep])
                       return false;
               }
               colstep += 1;
           }
           return true;
       }
    
    
    }
    
    public partial class MainWindow : Window
    {
        public int iN { get { return Convert.ToInt32(txtN.Text); } set { txtN.Text = "" + value; } }
        private bool[,] spelbord;
    
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            spelbord = new bool[iN, iN];
            NQueen.berekenQueens(0, iN, spelbord);
    
            visualise(iN, spelbord);
        }
    
        private void visualise(int N, bool[,] bord)
        {
            gridTekenen();
    
            for (int row = 0; row < N; row++)
            {
                for (int col = 0; col < N; col++)
                {
                    Rectangle rect = new Rectangle();
                    rect.Stretch = Stretch.Fill;
    
                    TextBlock txtB = new TextBlock();
    
                    if (spelbord[row, col])
                    {
                        SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                        mySolidColorBrush.Color = Colors.LightGreen;
                        rect.Fill = mySolidColorBrush;
    
                        txtB.Text = "Q";
                    }
    
                    rect.SetValue(Grid.ColumnProperty, col);
                    rect.SetValue(Grid.RowProperty, row);
    
                    txtB.SetValue(Grid.ColumnProperty, col);
                    txtB.SetValue(Grid.RowProperty, row);
    
                    gridPaneel.Children.Add(rect);
                    gridPaneel.Children.Add(txtB);
                }
            }
        }
    
        private void gridTekenen()
        {
            gridPaneel.ShowGridLines = true;
            int grooteGrid = int.Parse(txtN.Text);
    
            RowDefinition rowDef;
            ColumnDefinition colDef;
    
    
            for (int i = 0; i < grooteGrid; i++)
            {
                rowDef = new RowDefinition();
    
                GridLengthConverter myGridLengthConverter = new GridLengthConverter();
                GridLength gl1 = (GridLength)myGridLengthConverter.ConvertFromString(150 + "*");
                rowDef.Height = gl1;
    
                colDef = new ColumnDefinition();
                colDef.Width = gl1;
    
                gridPaneel.RowDefinitions.Add(rowDef);
                gridPaneel.ColumnDefinitions.Add(colDef);
            }
    
    
        }
    }