简体   繁体   English

CodeDom添加对现有文件的引用

[英]CodeDom add reference to existing file

In visual studio, I can click on "Reference" > "Add reference" and browse to an existing .dll file from my computer. 在visual studio中,我可以单击“Reference”>“Add reference”并从我的计算机浏览到现有的.dll文件。 I can then use the referenced dll as follows: 然后我可以使用引用的dll如下:

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

I know how to add a referenced assembly using codedom (will show this below), but the actual dll file is not being added to the project as it is when done through Visual Studio. 我知道如何使用codedom添加引用的程序集(将在下面显示),但实际的dll文件没有像通过Visual Studio一样添加到项目中。 Again, I need to be able to call some function in the dll file I'd like to reference. 同样,我需要能够在我想引用的dll文件中调用一些函数。

What I'm doing now: 我现在在做什么:

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

I'm assuming that I will need to do something different in order to have CodeDom add the dll to the output executable file. 我假设我需要做一些不同的事情才能让CodeDom将dll添加到输出可执行文件中。 What more needs to be done here? 还有什么需要在这里完成的?

Thanks for the help everyone! 感谢大家的帮助!

Following code may help you in loading the assembly and invoke method. 以下代码可以帮助您加载程序集和调用方法。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM