Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
First Google result for ".net load assembly dynamically": stackoverflow.com/questions/465488/can-i-load-a-ne...
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);
}
}
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 questionAsk a Question
731 491 924 answers to any question