简体   繁体   中英

C# Retrieving Information from DLL(Events)

I'm a Software Engineering student and we recently got an assignment where we have to simulate a hotel in C#. As extension to the simulation, the teachers deliver a DLL that contains certain events that we have to handle and apply to the simulation.

Our problem is that we have no idea how to retrieve the events from the DLL. We've tried to search for it on the internet but no answer applied to our situation.

The DLL is used as reference and we are able to call certain parts of the DLL so we do know the connection is right but we're lacking the knowledge of how to retrieve information.

Does anybody recommend certain websites to either learn about the subject or to solve the problem?

Also, very happy that stackoverflow exists, it has helped me with a lot of problems that luckily others already had. This is my first post.

You have 2 ways of executing, one for .NET based DLLs and one for non .NET based DLLs.

Non .NET:

public static class DllHelper
{
   [System.Runtime.InteropServices.DllImport("Dll1.dll")]
   public static extern int func1();
}

private void btnExec_Click(object sender, EventArgs e)
{
   try
   {
       DllHelper.func1();
   }
   catch (Exception ex)
   {
      Console.WriteLine(ex.Message);
   }
}

.NET based:

try
{
    System.Reflection.Assembly dll1 =  System.Reflection.Assembly.LoadFile("Dll1.dll");
    if (dll1 != null)
    {
        object obj = dll1.CreateInstance("Func1Cls");
        if (obj != null)
        {
            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("func1");
            mi.Invoke(obj, new object[0]);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

This is what I would try if I had some DLL files from which I also knew the methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM