1

Goal

I'm trying to use a dll file in a c# project. I know how to it with visual studio but is there any other way (without visual studio)? When I add dll file as reference into console app with visual studio, it does not add anything to Program.cs, But I think something should be added to it (and Program.cs of dll file I guess) when you don't want to use visual studio. For example what should I do when I want to use the following dll file:

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

namespace ClassLibrary1
{
    public class Class1
    {
        public static int Sum(int x, int y)
        {
            int a = x;
            int b = y;
            int result = a + b;
            return result;
        }
    }
}

in the following Console App:


using System;
using ClassLibrary1;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Class1.Sum(1, 40));
        }
    }
}

Edit

I mean what does visual studio to add a reference to project? (I want to do it without Visual Studio).

  • 1
    You totally confuse two different DLL types: native DLLs created with C and managed (.NET) dlls created with C#. Two first directives are for loading native DLLs but then you have an example of a managed DLL. – Wiktor Zychla May 09 '20 at 18:14
  • 1
    How are you compiling? Do you have a project file? If you have a project file, you can add references in the project file. If you are just trying to have multiple code files, you need a project. – Theraot May 09 '20 at 18:29
  • @WiktorZychla Thanks! edited –  May 09 '20 at 19:10

2 Answers2

1

Add the DLL reference into the csproj file.

Like (new csproj file format):

<ItemGroup>
    <ProjectReference Include="..\..\*.dll" />
</ItemGroup>
Krusty
  • 955
  • 1
  • 12
  • 26
0

This method will load the dll as below-

var DLL=Assembly.LoadFile(YourfilePath)
Samim Hussain
  • 396
  • 5
  • 14