0

So I created this dll file, and i am trying to call the methods dynamically

namespace getDirInf {

public static class fileInf
{

    public static long FileSizeRecursive(string strDirectory, long p_lnDirLength)
    {

        DirectoryInfo _dinf = new DirectoryInfo(strDirectory);
        DirectoryInfo[] _dirs = _dinf.GetDirectories();

        long _lnDirSize = p_lnDirLength;

        _lnDirSize += GetFileSize(strDirectory);

        if (_dirs.Length == 0)
        {
            return _lnDirSize;
        }
        else
        {
            foreach (DirectoryInfo dir in _dirs)
            {
                _lnDirSize = FileSizeRecursive(dir.FullName, _lnDirSize);
            }
            return _lnDirSize;
        }

    }

    public static long GetFileSize(string strDirectory)
    {

        long _lnDirSize = 0;
        DirectoryInfo _dinf = new DirectoryInfo(strDirectory);
        FileInfo[] dirFiles = _dinf.GetFiles();


        foreach (FileInfo fi in dirFiles)
        {
            _lnDirSize += fi.Length;
        }

        return _lnDirSize;

    }

}

}

And thats the function i'm using to call the methods in dll, this is the constructor

    public ThreadCall(string _path, TextBox txtSize)
    {
        this._path = _path;
        this.txtSize = txtSize;
        Assembly assembly = Assembly.LoadFile("C:\\...\\getDirInf.dll");
        var type = assembly.GetType("getDirInf.fileInf");

        MethodInfo method = type.GetMethod("FileSizeRecursive", BindingFlags.Public | BindingFlags.Static); 
    }

And this is the function call.

     public void callThread()
    {
        k = method.Invoke(null,  new object[] { _path, 0L }); //this is where i'm having problems
    }

Since the methods in dll are all static, i am trying to call method.Invoke with null as first parameter, and as second the parameters of function. I thought when the method is static, you don't need an instance in invoke and it would work with null but now i'm having null reference exception with this code...

Any help about how to solve this would be appreciated, thanks.

user3423295
  • 15
  • 1
  • 4
  • Set some breakpoints in the `ThreadCall` and `callThread` methods, start the application in the debugger and step through the code. For each step taken take a look at the variables in the watch window or by simply hovering over them. Are they what you expect them to be? – Dirk Jun 06 '14 at 09:00
  • Post the exception details with the Stacktrace. – H H Jun 06 '14 at 09:12
  • 1
    Are you sure you use the correct "method" object? in the constructor, is is a local variable, in the function it seems to be a member variable of the instance... – Bernhard Hiller Jun 06 '14 at 09:23
  • I realized the thread was working before the method's initialization, thanks Henk :) – user3423295 Jun 06 '14 at 09:29

1 Answers1

-1

This should help:

Assembly assembly = Assembly.LoadFile("C:\\...\\getDirInf.dll");
//var type = Type.GetType("getDirInf.fileInf");
var type = assembly.GetType("getDirInf.fileInf");

Calling Invoke(null, ...) is correct for a static method.

H H
  • 263,252
  • 30
  • 330
  • 514