Dismiss Notice

Register now to be one of the first members of this SharePoint Community! Click here it just takes seconds!

Dismiss Notice
Welcome Guest from Country Flag

How to make a simple DLL as an assembly reference just for fun

Discussion in 'Official Microsoft News' started by benjaminperkins, Apr 13, 2017.

Thread Status:
Not open for further replies.
  1. benjaminperkins

    benjaminperkins Guest

    Blog Posts:
    0
    The path to reproducing a scenario for a custom is sometimes a long-ish one. I needed to include a custom assembly in an Azure Function App which I describe here “How to add assembly references to an Azure Function App” and documented this one here “Could not load file or assembly” along the way here as well.

    Here are the steps you need to do, I also include how to add the reference to a Console applictio as well.

    • Create a Class Library project
    • Add the code, build and compile to make the DLL
    • Create a consumer Console application
    • Add the code and run it
    Create a Class Library project


    I used Visual Studio 2015 community, selected File –> New –> Project as shown in Figure 1.

    [​IMG]

    Figure 1, create a DLL in C# for referencing from a Console or Azure Function App

    Click the OK button and the pro´ject and solution are created for you.

    Add the code, build and compile to make the DLL


    Add / modify the existing code so that it resembles the following. The name space will be the name you provided for the project, leave that as is, it will also be the name of your DLL, for example, here the dll will be called benjamin.dll.


    using System;

    namespace benjamin
    {
    public class Greetings
    {
    public static string Hello(string name)
    {
    return $"Greetings {name}";
    }
    }
    }

    Name the class and add a method which does something. The class I named as Greetings which contained a method name Hello that accepts a string and returns the Greetings message. Presss CTRL + SHIFT+ B, F6 or select Build – Build Soluiont from the menu. Depending wheter you created a Debug or Release version of the dll, navigate to the project directory and you will find the dll, as shown in Figure 2.

    [​IMG]

    Figure 2, create a DLL in C# for referencing from a Console or Azure Function App

    Create a consumer Console application


    To consume the method in the dll from another program, you will need to add a reference to the assembly. But first, create the application that will consume the method. For example, create a Console application, as shown in Figure 3.

    [​IMG]

    Figure 3, consume a DLL in C# for referencing from a Console

    Then, as seen in Figure 4, add the reference to the dll by right-clicking on the Reference folder in the Console application than, Browse –> Browse… button –> navigate to the location of the dll –> select it and press the Add button.

    [​IMG]

    Figure 4, how to reference a DLL in C# from a Console

    Add the code and run it


    Lastly, add the using reference from within the console application and call the method, similar to that shown here.


    using System;
    using static System.Console;
    using benjamin;

    namespace consume_benjamin
    {
    class Program
    {
    static void Main(string[] args)
    {
    WriteLine("What is your name? ");
    var name = ReadLine();
    if (name != null)
    {
    WriteLine(benjamin.Greetings.Hello(name));
    }
    else
    {
    WriteLine("You must enter a name, good bye");
    }

    ReadLine();
    }
    }
    }

    Press F5, enter a Name and the code is called and displyed as seen in Figure 5-

    [​IMG]

    Figure 5, how to reference a DLL in C# from a Console

    Continue reading...
     
Thread Status:
Not open for further replies.

Share This Page

LiveZilla Live Chat Software