Search code examples
c#dll.net-assembly

issue with loading .dll lib in C# code


I have found a few posts about loading .dll libraries directly into C# projects through code. Here is the current code I am using:

            if (System.IO.File.Exists(dir + lib + ".dll"))
            {
                Assembly type = Assembly.LoadFrom(dir + lib + ".dll");
                object obj1 = type.CreateInstance(_namespace);



                assm.Add(obj1);
                return assm.Count - 1;
            }
            else
            {
                Console.WriteLine(dir + lib + ".dll: file does not exist");
                return -1;
            }

The code with:

                Assembly type = Assembly.LoadFrom(dir + lib + ".dll");
                object obj1 = type.CreateInstance(_namespace);

is suppose to locate the .dll file and load it as an instance then grabbing the class and creating an object from it. So I could make a library that has one function to print hello and I then load it into the code, I could then access that function by creating an Instance of the class. The problem is when I do the code:

                object obj1 = type.CreateInstance(_namespace);

obj1 is null, I know to give the namspace to the function (which I double checked and is correct), but no matter what it is always null. I checked to see if the Library is being loaded in as an assembly and it is! Here is the details of the assembly when I do a breakpoint (hovering over the object type):

    {ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}

Thank you in advance for your help, I am not sure what is wrong with my code.


Solution

  • What is _namespace? It must contents full name of class (with namespace), for example System.Collections.ArrayList