1

I am trying to create a new app domain to convert document, but I am not getting familiar to the syntax of it, this is what I am trying to convert a document,

 Aspose.Words.Document doc = new Aspose.Words.Document(inputFileName);
 doc.Save(Path.ChangeExtension(inputFileName, ".pdf"));

I want to run above code in new APP domain so I am trying this,

 AppDomain domain = AppDomain.CreateDomain("New domain name");
 string pathToDll = @"C:\Users\user1\Desktop\Aspose.Words.dll";
 Type t = typeof(Aspose.Words.Document);
 Aspose.Words.Document myObject = (Aspose.Words.Document)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

But how can I create a constructor as I am doing in 1st code spinet...

Mathematics
  • 7,314
  • 25
  • 77
  • 152

1 Answers1

3

you can use an overloaded definition of AppDomain.CreateInstanceFromAndUnwrap() that accepts additional parameters, an array of objects to pass through to the constructor amongst them. Your code could then look like this:

AppDomain domain = AppDomain.CreateDomain("New domain name");
string pathToDll = @"C:\Users\user1\Desktop\Aspose.Words.dll";
Type t = typeof(Aspose.Words.Document);
Object[] constructorArgs = new Object[1];
constructorArgs[0] = inputFileName;
Aspose.Words.Document myObject = (Aspose.Words.Document)domain.CreateInstanceFromAndUnwrap(
    pathToDll, 
    t.FullName,
    false,            //ignoreCase
    0,                //bindingAttr
    null,             //binder, use Default 
    constructorArgs,  //the constructor parameters
    null,             //culture, use culture of current thread
    null);            //activationAttributes
Dirk Trilsbeek
  • 5,873
  • 2
  • 25
  • 23
  • Thanks, +1, it might not be related to answer but this is error i am getting..Description: Type 'Aspose.Words.Document' in assembly 'Aspose.Words, Version=14.10.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56' is not marked as serializable. – Mathematics Dec 23 '14 at 09:32
  • I'm not an expert on AppDomains either, but it seems if you want to use a class from a different AppDomain in your own Appdomain, it has to be marked as serializable. The other option would be to subclass the class from MarshalByRefObject and use it in its own AppDomain. See this SO thread: http://stackoverflow.com/questions/599731/use-the-serializable-attribute-or-subclassing-from-marshalbyrefobject – Dirk Trilsbeek Dec 23 '14 at 10:44