c# - MissingMethodException 未处理

标签 c# reflection missingmethodexception

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

        // 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);     

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

    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);     
    }

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

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;
}
}

谁能帮我解决这个问题吗?

最佳答案

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

      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 */
                    });

关于c# - MissingMethodException 未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538196/

相关文章:

.net - 如何通过反射获取某个 ValueType 的 MinValue/MaxValue?

asp.net-mvc - 没有为此对象定义无参数构造函数。

c# - C# 程序中的 MissingMethodException

c# - 尽管已经执行了依赖于 Func`2 的代码,但 WinCE 上的 Func`2<> 出现 MissingMethodException

c# - 填充 ListBox 时从每个列表项中删除前 12 个字符

c# - Timer.Interval 问题

java - 如何从 genericReturnType 创建实例

c# - Jquery 运行时错误 : object expected

c# - 尝试使用 Dialogflow V2 API 中的自定义事件更新意图参数,但它没有传递到意图

C# VS 2005 : How to get a class's public member list during the runtime?