A
A
askeet2014-05-23 13:06:02
C++ / C#
askeet, 2014-05-23 13:06:02

Dynamic linking of C# libraries in .NET

How to dynamically link an external library without using Referenses?
It is necessary to implement the task if the library file is missing to compile the project without it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
trerums, 2014-05-23
@trerums

First Google result for ".net load assembly dynamically": stackoverflow.com/questions/465488/can-i-load-a-ne...

A
askeet, 2014-05-26
@askeet

There are 2 projects with one interface, one or another program that dynamically
connects it.
For some reason the program considers that they are incompatible.
You can, of course, make an intermediate dll with an interface, but I can't do it.
Help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibClass
{
    public  interface IClass
    {
         void Write();
         int Ref(ref int f);
    }
}

At the moment I solved the problem, only through string access, but I think this is not very optimal.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;



namespace My_Project.Trace
{

    class IncludeLibrary
    {
        public IncludeLibrary(string PathDll)
        {
            if (System.IO.File.Exists(PathDll))
            {
                Assembly Ass = Assembly.LoadFrom(PathDll);
                ListTypes = Ass.GetTypes(); // Чтение всех типов
                isValid = true;
            }
            else
            { 
                Console.WriteLine("Dll не найдена:" + PathDll);
            }
        }
        public bool isValid = false;

        public static bool operator ==(IncludeLibrary LibA, IncludeLibrary LibB)
        {
            if ((object)LibA != null && !LibA.isValid)
            {
                LibA = null;
            }

            if ((object)LibB != null && !LibA.isValid)
            {
                LibB = null;
            }
            return object.Equals(LibA, LibB);
        }

        public static bool operator !=(IncludeLibrary LibA, IncludeLibrary LibB)
        {
            return !(LibA == LibB);
        }


        public object DllClass(string FindClass, params object[] argsClass)
        {
            findDllClass = null;
            if (ListTypes != null)
            {
                foreach (Type type in ListTypes)
                {
                    if (type.FullName.Equals(FindClass))
                    {
                        findClassType = type;
                        if (argsClass != null)
                        {
                            findDllClass = Activator.CreateInstance(findClassType, argsClass);
                        }
                        else
                        {
                            findDllClass = Activator.CreateInstance(findClassType);
                        }
                    }
                }
                if (findDllClass == null)
                {
                    Console.WriteLine("Plugin"+ FindClass + " not find");
                }
            }
            return findDllClass;
        }


        /// <summary>
        ///  Запустить метод класса, найденный при вызове DllClass
        /// </summary>
        /// <param name="function"></param>
        /// <param name="argsClass"></param>
        /// <returns></returns>
        public object Method(string function, params object[] argsClass)
        {
            return Method(findDllClass, function, argsClass);
        }

        public object Method(object Class, string function, params object[] argsClass)
        {
            object ret = null;
            MethodInfo mi = null;
            bool InvokeCorrect = false;
            try
            {
                if (Class != null)
                {
                    mi = Class.GetType().GetMethod(function);
                    if (mi != null)
                    {
                        ret = mi.Invoke(Class, argsClass);
                        InvokeCorrect = true;
                    }
                }
            }
            catch
            {
                ret = null;
            }

            if (!InvokeCorrect)
            {
                Console.WriteLine(String.Format("{0}.{1} error in Plugin {2}", (Class != null) ? Class.GetType().Name : "null", function, (mi == null) ? "or function not find" : ""));
            }
            return ret;
        }
        object findDllClass = null;
        Type findClassType = null;
        Type[]  ListTypes = null;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question