在C#中从DLL调用程序中的方法

galaxy001

我想从我的dll中调用方法。
我现在可以添加一个参数public void Bar(Action<string> print, string a)但是还有其他方法可以调用吗?
dll中的代码:

class Foo {
    public void Bar(string a) {
        //now I want to call method from program
        Program.Print(a); // doesn't work
    }
}

程序:

class Program {
    public static void Main(string[] args) {
        var dll = Assembly.LoadFile(@"my dll");
        foreach(Type type in dll.GetExportedTypes())
        {
            dynamic c = Activator.CreateInstance(type);
            c.Bar(@"Hello");
        }
        Console.ReadLine();
    }
    public static void Print(string s) {
        Console.WriteLine(s);
    }
}

from(在运行时在C#中加载DLL
是否可能?

galaxy001

在DLL中,我添加了对程序的引用,并且该代码现在可以正常工作。
dll中的代码:

class Foo {
    public void Bar(string a) {
        Program.Print(a);
    }
}

程序:

class Program {
    public static void Main(string[] args) {
        var dll = Assembly.LoadFile(@"my dll");
        foreach(Type type in dll.GetExportedTypes())
        {
            dynamic c = Activator.CreateInstance(type);
            c.Bar(@"Hello");
        }
        Console.ReadLine();
    }
    public static void Print(string s) {
        Console.WriteLine(s);
    }
}

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章