mercredi 7 décembre 2016

How to invoke instance method from class library in different appdomain?

I know that there are many similar answers but none of them suit for me. I have a class library called MyLibrary. It has only one type.

public class Test
    {
        public Test()
        {
            Console.WriteLine("Ctor of Test type.");
        }

        public void Hello(string name)
        {
            Console.WriteLine($"Hello {name}! I'm an instance method.");
        }
    }

I created a simple console application. This is a code of Main method.

static void Main(string[] args)
        {
            string path = @"example of my path";
            AppDomain domain = AppDomain.CreateDomain("mydomain");
            Assembly mylibrary = Assembly.LoadFrom(path);
            Type typeOfTest = mylibrary.GetType("MyLibrary.Test");
            var instanceOfTest = domain.CreateInstanceFrom(path, typeOfTest.FullName);
            MethodInfo hello = typeOfTest.GetMethod("Hello");
            hello.Invoke(instanceOfTest, new object[] {"Bob"});
        }

What is the right way to call Hello method? I can create and call static method from Test type but I can do nothing with nonstatic instance method?





Aucun commentaire:

Enregistrer un commentaire