0

I have my DLL:

using System;
namespace DLLtest
{
    public static class TestDll
    {
        public static void TestVoid()
        {
            Console.WriteLine("TestVoid called");
        }
    }
}

and in my program I'm doing:

Assembly a = Assembly.LoadFile(
     Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DLLtest.dll"));
var b = a.GetType("TestDll").GetMethod("TestVoid");
b.Invoke(null, new object[] { });

And I get NullReferenceException on "var b..." line (indeed something is null as What is a NullReferenceException, and how do I fix it? explains, but assembly is loaded and class should be there).

I tried adding BindingFlags, but always the same error...

Community
  • 1
  • 1
TheChilliPL
  • 337
  • 1
  • 5
  • 14
  • 1
    I dont think the supposed duplicate question has anything to do with the OP's error here. – rafaelc Sep 20 '16 at 15:53
  • @RafaelCardoso That null reference duplicate gets way overused. – LarsTech Sep 20 '16 at 15:55
  • I think OP will also need to pass `a` as the first arg to the `Invoke` call, or the `Invoke` won't know which object to call the method on. EDIT: this advice is incorrect, method in question example is static. – cskwrd Sep 20 '16 at 15:56
  • 1
    @TheGNUGuy or you may want to read on how to call static methods with reflection :) – Alexei Levenkov Sep 20 '16 at 15:57

1 Answers1

4

The name of the type is DLLTest.TestDll - so there you get null searching for just TestDll.

Fix:

  • use full class name
  • get all types from assembly and find type by matching part of the name.
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179