3

I have this library to compiled to calc.dll.

namespace MyClass
{
    public class Calculator
    {
        public int Value1 {get; set;}
        public int Value2 {get; set;}
        public Calculator()
        {
            Value1 = 100;
            Value2 = 200;
        }

        public int Add(int val1, int val2)
        {
            Value1 = val1; Value2 = val2;
            return Value1 + Value2;
        }
    }
}

I want to instantiate the Calculate class without linking to the calc.dll. Can C# do that? I came up with this code, but I don't know how to instantiate the Calculator class.

using System;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections.Generic;

namespace EX
{
    public class Code
    {
        public static void Test()
        {
            string path = Directory.GetCurrentDirectory();
            string target = Path.Combine(path, @"./myclass.dll");
            Assembly asm = Assembly.LoadFrom(target);

            Calculator h = new Calculator(); // <-- ???
            Type type = h.GetType();
            MethodInfo m = type.GetMethod("Add");

            int res = (int) m.Invoke(h, param);
            Console.WriteLine("{0}", res);
        }

        public static void Main()
        {
            Test();
        }
    }
}

ADDED

I have two solutions, one is from Bala R

        var param = new object[] {100, 200};
        string path = Directory.GetCurrentDirectory();
        string target = Path.Combine(path, @"./myclass.dll");            
        Assembly asm = Assembly.LoadFrom(target);            
        Type calc = asm.GetType("MyClass.Calculator");
        object h  = Activator.CreateInstance(calc);         

        MethodInfo m = calc.GetMethod("Add");            
        int res = (int) m.Invoke(h, param);            
        Console.WriteLine("{0}", res); 

And this one is from agent-j

        string path = Directory.GetCurrentDirectory();
        string target = Path.Combine(path, @"./myclass.dll");
        Assembly asm = Assembly.LoadFrom(target);
        Type type = asm.GetType("MyClass.Calculator");
        ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
        object calc = ctor.Invoke(null);
        MethodInfo m = type.GetMethod("Add");

        var param = new object[] {100, 200};

        int res = (int) m.Invoke(calc, param);
        Console.WriteLine("{0}", res);

Both of them are working, but I kinda prefer Bala's solution as it's shorter and getting object h through CreateInstance is more intutive than getting constructor to get object h(calc).

2 Answers 2

7
string path = Directory.GetCurrentDirectory();
string target = Path.Combine(path, @"./myclass.dll");
Assembly asm = Assembly.LoadFrom(target);
Type type = asm.GetType("MyClass.Calculator");
ConstructorInfo ctor = type.GetConstructor(Type.EmptyTypes);
object calc = ctor.Invoke (null);
MethodInfo m = type.GetMethod("Add");

int res = (int) m.Invoke(calc, param);
Console.WriteLine("{0}", res);      
Sign up to request clarification or add additional context in comments.

2 Comments

How to get the h in m.Invoke(h, param)?
@prosseek, ` object calc = ctor.Invoke (null); MethodInfo m = type.GetMethod("Add"); int res = (int) m.Invoke(calc, param); `
4
object h = Activator.CreateInstance(asm.FullName, "MyClass.Calculator");

EDIT:

See if this works

Type calc = asm.GetType("MyClass.Calculator)";
object h  = Activator.CreateInstance(calc);

2 Comments

It compiles, but I got Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at EX.Code.Test() at EX.Code.Main() error.
@prosseek see my edit. I have never used the first mentioned overload of CreateInstance(). I'm a little doubtful abou the asm.FullName but I have used the second method, and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.