Question

I'm creating an assembly via reflection. When I try to run my application I get an MissingMethodExeption on :

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

My code (When clicked on the menuItem I want to create an assembly and load the classes)

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

The methods I want to load come from my project 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;
}
}

Can anyone help me with this?

Was it helpful?

Solution

The binder uses the arguments as well to find a suitable method. You have no method void berekenQueens() therefor calling InvokeMember with null as the last parameter (the arguments array) will not give a matching Method. You don't really need the instance (because the method is static) so you leave it null if you wish.

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

OTHER TIPS

use BindingFlags.NonPublic in place of BindingFlags.Instance for method bordValidatie as it is a private method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top