Using the built-in DLL?

Is there a detailed guide on using the built-in dll library in a C # source? All the guides that I find on Google seem to be of little help. All this is "create a new class" or "ILMerge" and ".NETZ". But I'm not sure how to use ILMerge and .NETZ materials, and the class guides do not take into account what to do after creating the class file, since after that I find nothing new. For example, this . After adding the class and function, I have no idea how to contact the dll with my resources.

Thus, I would like to learn how to use a file .dllthat was embedded into Resourcesin order to be able to call class, without any parts. Please keep in mind that I am not very good at coding in C #. Thanks in advance .: D

PS. Try not to use these big words. I am easily lost.

+3
source share
2 answers

You may need to use ILMerge. It seems like this will solve the problem naturally for you.

http://research.microsoft.com/en-us/people/mbarnett/ILMerge.aspx

+2
source

Stream DLL, Assembly.GetManifestResourceStream, , - , Assembly.Load ( , , - Assembly.Load Assembly.LoadFile, ).

.. - , Assembly.Load ( ). "CLR via #" .

, ? , ... , ?

, :

// DemoLib.cs - we'll build this into a DLL and embed it
using System;

namespace DemoLib
{   
    public class Demo
    {
        private readonly string name;

        public Demo(string name)
        {
            this.name = name;
        }

        public void SayHello()
        {
            Console.WriteLine("Hello, my name is {0}", name);
        }
    }
}

// DemoExe.cs - we'll build this as the executable
using System;
using System.Reflection;
using System.IO;

public class DemoExe
{
    static void Main()
    {
        byte[] data;
        using (Stream stream = typeof(DemoExe).Assembly
               .GetManifestResourceStream("DemoLib.dll"))
        {
            data = ReadFully(stream);
        }

        // Load the assembly
        Assembly asm = Assembly.Load(data);

        // Find the type within the assembly
        Type type = asm.GetType("DemoLib.Demo");

        // Find and invoke the relevant constructor
        ConstructorInfo ctor = type.GetConstructor(new Type[]{typeof(string)});
        object instance = ctor.Invoke(new object[] { "Jon" });

        // Find and invoke the relevant method
        MethodInfo method = type.GetMethod("SayHello");
        method.Invoke(instance, null);
    }

    static byte[] ReadFully(Stream stream)
    {
        byte[] buffer = new byte[8192];
        using (MemoryStream ms = new MemoryStream())
        {
            int bytesRead;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, bytesRead);
            }
            return ms.ToArray();
        }
    }
}

:

> csc /target:library DemoLib.cs
> csc DemoExe.cs /resource:DemoLib.dll
+4

Source: https://habr.com/ru/post/1725020/


All Articles