Michel L
Michel L

Reputation: 535

How to instantiate a class that is declared in a C# DLL?

I have a DLL written in C#. In this DLL there is a class that is defined. Let's assume that the code of this DLL is :

namespace unloadableDLL
{
    public class DivisionClass
    {
        public /*static*/ long Division(long x, long y)
        {
            // We deliberately do not use a try catch to catch divide by 0 exceptions
            return (x / y);
        }
    }
}

Now, I want to load dynamically this DLL in a test program. I need to see what happens if I divide by zero in the two following cases : 1) DLL is loaded directly (without using an AppDomain) 2) DLL is not loaded directly, an AppDomain is created first and then it loads the DLL.

I'm completely new in C#, by new I mean I've started less than 4 hours ago but I have a C++ background.

My problem is that in my test program, I need to instanciate a DivisionClass Object but this one is declared only in the DLL. => Resolved

My test program is

class Program
    {
        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
        static void Main(string[] args)
        {
            // Load the DLL
            Console.WriteLine("Attempting to load unloadableDLL");
            Assembly a = Assembly.LoadFrom("./unloadableDLL.dll");
            unloadableDLL.DivisionClass divisionObject = (unloadableDLL.DivisionClass)a.CreateInstance("unloadableDLL.DivisionClass");
            long number = divisionObject.Division(8, 2);
            Console.WriteLine(number);
        }
    }

I don't know what but the compilers keeps telling me that Static member unloadableDLL.DivisionClass.Division(lon, long) cannot be accessed with an instance reference; qualify it with a typename instead.

Thank you all

Upvotes: 3

Views: 6134

Answers (1)

Crab Bucket
Crab Bucket

Reputation: 6277

To load into a separate AppDomain an execute the method from there - use reflection as in the following StackOverflow question. It does get involved as is typically a more advanced C# topic but the code is fairly boilerplate and for a man with C++ knowledge it shouldn't present problems.

For a direct call then follow the methods outlined above - i.e. reference the dll in your project and instantiate through code as per Oded (who has deleted his answer unfortunately so reproducing below)

DivisionClass.Division(1, 2)

EDIT

If the methods are not static

Invoking a method via reflection

  Assembly myAssembly1 = Assembly.LoadFrom("myPath\\Assembly1.dll");     
  Type myType = myAssembly1.GetType("MyClass");      
  object myObject = Activator.CreateInstance(myType);     
  myType.Invoke("myMethodName", BindingFlags.InvokeMethod, null, myObject, null); 

In a separate App Domain adds to the complexity - see link.

Also I don't think your Division class will work as is. To call across app domains you need to employ some kind of serialisation for your classes or inherit the class from MarshalByRefObject - see this SO question. Given that this looks like a proof of concept type of thing/a ground up implementation of your class then MarshalByRefObject would be you best bet - easiest i think. That said executing across AppDomains can get fiddly.

Invoking a method via instaniation in code

DivisionClass divisor = new DivisionClass()
divisor.Division(1,2)

Upvotes: 4

Related Questions