C#动态DLL调用gettype

用户名

所以我创建了这个dll文件,我试图动态调用这些方法

命名空间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;

    }

}

}

那就是我用来调用dll中的方法的函数,这是构造函数

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

这就是函数调用。

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

由于dll中的方法都是静态的,因此我尝试调用method.invoke,将null作为第一个参数,并将第二个作为函数的参数。我以为方法是静态的时,您不需要在实例中调用并且它将与null一起工作,但是现在我在此代码中具有null引用异常...

感谢您提供有关如何解决此问题的帮助。

亨克·霍尔特曼

这应该有助于:

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

Invoke(null, ...)对于静态方法,调用是正确的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章