CodeDom添加对现有文件的引用

时间:2012-01-06 18:09:55

标签: c# reference codedom

在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添加到输出可执行文件中。还需要做些什么?

感谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

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

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