Search code examples
c#reflection.net-assemblymarshalbyrefobject

how to use MarshalByRefObject to call class in different app domain


I am trying to implement a version of example 1 from here http://msdn.microsoft.com/en-us/library/System.MarshalByRefObject(v=vs.110).aspx into my code.

My aim is to have a class with some methods then load that class into a different appdomain and call its method.

so far I have:

 public class diffDomain : MarshalByRefObject
 {
      public int getNumber()
      {
           return 5;
      }
 }
 internal static class JITCompiler
 {
      internal static wantNumber()
      {
           AppDomain domain = AppDomain.CreateDomain("MyDomain");
           var newSearch = (diffDomain)domain.CreateInstanceAndUnwrap(
                            Assembly.GetExecutingAssembly().FullName,
                            "diffDomain");
      }
 }

I get an error on the Var newSearch line:

Could not load type 'diffDomain' from assembly 'SVM, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

Solution

  • Your assembly/namespace could be incorrect. To avoid such errors try this:

    var newSearch = (diffDomain)domain.CreateInstanceAndUnwrap(
                                typeof(diffDomain).Assembly.FullName,
                                typeof(diffDomain).FullName);