在 Visual Studio 中,我可以单击“引用”>“添加引用”并从我的计算机浏览到现有的 .dll 文件。然后我可以使用引用的 dll 如下:

dllNameSpace.dllClassName myReference = new dllNameSpace.dllClassName();
myReference.someVoid();

我知道如何使用 codedom 添加引用的程序集(将在下面显示),但实际的 dll 文件并未像通过 Visual Studio 完成时那样添加到项目中。同样,我需要能够在我想引用的 dll 文件中调用一些函数。

我现在在做什么:
            // Configure a CompilerParameters that links the system.dll and produces the specified executable file.
            string[] referenceAssemblies = {
                                            "System.dll",
                                            "System.Drawing.dll",
                                            "System.Windows.Forms.dll",
                                            "System.Data.dll",
                                            "System.Xml.dll",
                                            "System.Management.dll",
Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\myDllFile.dll"
                                           };

            CompilerParameters cp = new CompilerParameters(referenceAssemblies, exeFile, false);

我假设我需要做一些不同的事情才能让 CodeDom 将 dll 添加到输出可执行文件中。这里还需要做什么?

感谢大家的帮助!

最佳答案

以下代码可以帮助您加载程序集和调用方法。

        Assembly asmbly = Assembly.LoadFile("assembly.test.dll");
        var myclass = asmbly.GetType("MyClass"); // use FullName i.e. Namespace.Classname
        var myobj = Activator.CreateInstance(myclass);
        myclass.GetMethod("MyMethod").Invoke(myobj,new object[]{"param1","param2"});

关于c# - CodeDom 添加对现有文件的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8762398/

10-12 12:45
查看更多