Reputation: 88
Good day, I've got following problem. I'm working on my .dll library in C#, and I need to call a method of another C# project from the dll library. For example I create a WPF project and I add reference of my .dll library.
Here is my WPF class (project):
using dllLibrary;
namespace Tester
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void MyMethod()
{
MessageBox.Show("Test");
}
}
}
And here is my .dll project :
Type type = Type.GetType("Tester.MainWindow");
object instance = Activator.CreateInstance(type, null);
MethodInfo method = type.GetMethod("MyMethod");
return method.Invoke(instance, null);
By the way, it works when I call a method inside of the assembly (of the dll project), but when I want to call a method outside of the dll project (Tester.MainWindow - the WPF project), it doesn't work.
Upvotes: 0
Views: 1646
Reputation: 237
Type.GetType without the full qualified name incl. assembly only works for types in mscorlib.dll. For all other types you have to pass the full qualified name of the type. So when you change the GetType call to something like this it will work:
Type.GetType("Tester.MainWindow, TestAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089);
You can load the assembly first using Assembly.LoadFile to get the full qualified name.
Upvotes: 2