We have an asp.net 4.7 project and we're trying to call a method in another dll file ( from one of the dll files inside /Bin )
This is how the project structure looks:
Our Main project
- Sub project: Asp.net Project - hosted on a webserver
- Another sub project called WA.Core.Framework
- Another sub project called WA.Core.Libary
- Another sub project callde WA.Extension.Pages
When compiling the project all the files will automatically be copied to the Asp.net project folder /Bin
In our Library project we have the invoke method:
public string InvokeString(string typeName, string methodName)
{
Type calledType = Type.GetType(typeName);
String s = (String)calledType.InvokeMember(
methodName,
BindingFlags.InvokeMethod | BindingFlags.Public,
null,
null,
null);
return s;
}
And in the framework project we have the function that calls the method we want to call:
new Lib_Invoke().InvokeString("WA.Extension.Pages.Extension", "Init");
And last in our WA.Extension.Pages we have:
namespace WA.Extension.Pages
{
public class Extension
{
public void Init()
{
HttpContext.Current.Response.Write("hello from extension");
}
}
}
But so far all it gives me is
System.NullReferenceException: Object reference not set to an instance of an object.
I have double checked the references and should work.
EDIT:
After playing around with it and with the help from this thread, I ended up with this:
public string InvokeString(string assemblyName, string namespaceName, string typeName, string methodName)
{
Type calledType = Type.GetType(namespaceName + "." + typeName + "," + assemblyName);
String s = (String)calledType.InvokeMember(
methodName,
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
null,
Activator.CreateInstance(calledType),
null);
return s;
}
One of the important things that was added was the asambly name and Activator.CreateInstance(calledType) in the bindingflags.
So when I want to call the method:
new Lib_Invoke().InvokeString("WA.Extension.Pages", "WA.Extension.Pages", "Extension", "Init");
Thanks for your help!