4

In visual studio, I can click on "Reference" > "Add reference" and browse to an existing .dll file from my computer. I can then use the referenced dll as follows:

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. Again, I need to be able to call some function in the dll file I'd like to reference.

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. What more needs to be done here?

Thanks for the help everyone!

  • This works fine in Visual Studio, I'm trying to get the same effect using Codedom. –  Jan 06 '12 at 18:17
  • Is there a compile function or method that you must Invoke once you use that.. I know that CompilerParameters has 4 overloads and what you are doing looks correct.. but when are you actually creating the dll ? looks like your just adding it to cp via the referenceAssemblies string array this MSDN Site will also help server as a Reference aside from @Manas great example http://msdn.microsoft.com/en-us/library/system.codedom.compiler.compilerparameters.aspx – MethodMan Jan 06 '12 at 18:27
  • The dll file already exists prior to my CodeDom code running. –  Jan 06 '12 at 18:31
  • I did it the same way you did it, and it worked like a charm for me, it is just like add it in the IDE, i was able to use the functions inside the `dll` file!!! – Mousa Alfhaily Jul 21 '17 at 13:00

1 Answers1

0

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"});
Manas
  • 2,534
  • 20
  • 21
  • Yes, I understand how to load and invoke - I do not want to do this though. In Visual Studio I can simply reference a dll and call it. Why can't I have this same functionality using CodeDom? –  Jan 06 '12 at 18:28