首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MissingMethodException未处理

MissingMethodException未处理
EN

Stack Overflow用户
提问于 2013-01-26 23:08:47
回答 2查看 2.6K关注 0票数 2

我正在通过反射创建一个程序集。当我尝试运行我的应用程序时,我得到一个MissingMethodExeption:

代码语言:javascript
复制
        // public static bool berekenQueens(int Row, int N, bool[,] bord)
        objType.InvokeMember("berekenQueens",
            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static,
            null, instance, null);

        // private static bool bordValidatie(int currentRow, int currentCol, bool[,] currentBord, int N)
        objType.InvokeMember("bordValidatie",
            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static,
            null, instance, null);     

我的代码(当单击menuItem时,我想创建一个程序集并加载类)

代码语言:javascript
复制
    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        // Create an assembly object to load our classes
        string path = System.Environment.CurrentDirectory + "\\NQueens.dll";
        Assembly ass = Assembly.LoadFile(path);
        Console.WriteLine(path);

        Type objType = ass.GetType("NQueens.NQueen");

        // Create an instace of NQueens.NQueen
        var instance = Activator.CreateInstance(objType);

        // public static bool berekenQueens(int Row, int N, bool[,] bord)
        objType.InvokeMember("berekenQueens",
            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static,
            null, instance, null);

        // private static bool bordValidatie(int currentRow, int currentCol, bool[,] currentBord, int N)
        objType.InvokeMember("bordValidatie",
            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Static,
            null, instance, null);     
    }

我想要加载的方法来自我的项目NQueens。

代码语言:javascript
复制
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;
}
}

有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-26 23:28:28

绑定器也使用参数来找到合适的方法。您没有方法berekenQueens(),因此调用最后一个参数为null的InvokeMember (参数数组)将不会给出匹配的方法。您并不真正需要该实例(因为该方法是静态的),因此如果您愿意,可以将其保留为null。

代码语言:javascript
复制
      Type objType = ass.GetType("NQueens.NQueen");
      // Create an instace of NQueens.NQueen
      var instance = Activator.CreateInstance(objType);

    var result = objType.InvokeMember("berekenQueens", 
    BindingFlags.InvokeMethod | 
    BindingFlags.Static | 
    BindingFlags.Public,
      null, 
      instance, 
    new object[] {   1, /* Row */ 
                     1, /* N */
                     new bool[,] { {true,false} } /* bord */
                    });
票数 3
EN

Stack Overflow用户

发布于 2013-01-26 23:18:03

使用BindingFlags.NonPublic代替方法bordValidatieBindingFlags.Instance,因为它是私有方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14538196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档