S
S
Stepan Gervik2019-03-19 15:24:30
C++ / C#
Stepan Gervik, 2019-03-19 15:24:30

What is the problem with dynamic DLL linking?

Good afternoon! This is the problem:
1) I have crooked hands
2) I'm trying to deal with libraries in C #
3) the dll is called ConsoleClient.dll
3) The bottom line is this: I connect the dll dynamically

Assembly a = Assembly.Load("ConsoleClient");
            Object o = a.CreateInstance("Program");
            Type t = a.GetType("Program");

            Object[] numbers = new Object[2];
            numbers[0] = 2;
            numbers[1] = 3;
            MethodInfo mi = t.GetMethod("add");
            Console.WriteLine(mi.Invoke(o, numbers));
            Console.ReadLine();

Here is the dll itself
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
public class Program
{
    public int add(int a, int b)
    {
        return a + b;
    }
}

Well, during the launch of the program, an error pops up.
5c90dfb61bf77014126665.png
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan Gervik, 2019-03-19
@HiLevel

Assembly a = Assembly.LoadFrom("ConsoleClient.dll");
            Type[] types = a.GetTypes();
            foreach (Type z in types)
                Console.WriteLine("--> " + z);
            Console.WriteLine();
            Object o = a.CreateInstance("Program");
            Type t = a.GetType("ConsoleClient.Program");

            Object[] numbers = new Object[2];
            numbers[0] = 2;
            numbers[1] = 3;
            MethodInfo mi = t.GetMethod("add");
            Console.WriteLine(mi.Invoke(o, numbers));
            //чтобы консоль мгновенно не закрылась
            Console.ReadLine();

Something like this

#
#, 2019-03-19
@mindtester

What am I doing wrong?
all
if you had a C/C++ DLL - there would be a reason and a solution.. for dotnet - you just need to include the library project in the solution (solution)
.. compare with how nuget packages are included, this is a sample solution
pps Alexey Pavlov , Stepan Gervik ,
I put it wrong, by the phrase "only assembly connection" I meant just dynamic connection of the assembly, either through Assembly.Load or AppDomain.Load ()
+
The goal is for the dll to be loaded from the server and exist only in RAM, so such dances with a tambourine
.. well, let's start with the fact that you need to have correctly compiled versions of the DLL for all possible client OS .. this is initially the choice of the path of terrible hemorrhoids .. I think this can be finished))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question