mercredi 21 octobre 2015

Call C# Method from C# Dll using Reflection

Good day, I 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 WPF project and I add reference of my .dll library.

There is my WPF class (project):

using dllLibrary;

namespace Tester
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void MyMethod()
        {
            MessageBox.Show("Test");
        }
    }
}

and There 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 doesnt work.





Aucun commentaire:

Enregistrer un commentaire