Wednesday, May 25, 2016

Call external DLL from c# using reflection

using System;
using System.Collections.Generic;
using System.Reflection;
using DTOTest;

namespace TestLoadDLL
{
    class Program
    {
        public static string dllRoot = @"......\DLLTest.dll";
        public static string assemblyName = "DLLTest.GenerateERP";
        public static string methodName = "AddInfo";
        static void Main(string[] args)
        {
            List lstObjects = new List();
            lstObjects.Add(new ObjectDTO());
            lstObjects.Add(new ObjectDTO());
            lstObjects.Add(new ObjectDTO());
            lstObjects.Add(new ObjectDTO());

            Assembly a = Assembly.LoadFile(dllRoot);
            Type type = a.GetType(assemblyName);

            if (type != null)
            {
                MethodInfo methodInfo = type.GetMethod(methodName);
                object classInstance = Activator.CreateInstance(type, null);
                if (methodInfo != null)
                {
                    var result = (bool)methodInfo.Invoke(classInstance, new object[]
                        {
                            lstObjects
                        });
                }
            }

        }
    }
}


------------------------------------------------

using DTOTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLLTest
{
    public class GenerateERP : IGenerateERP
    {
        public bool AddInfo(List lstObjects)
        {
            if (lstObjects.Count > 1)
                return true;
            else
                return false;
        }
    }
}

No comments:

Post a Comment