允许我是app域的新用户,如何使用app域运行构造函数

时间:2014-12-23 09:09:21

标签: c# appdomain

我正在尝试创建一个新的应用程序域来转换文档,但我对它的语法并不熟悉,这就是我想要转换文档的内容,

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

我想在新的APP域中运行上面的代码,所以我正在尝试这个,

 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);

但是我如何创建一个构造函数,正如我在第一个代码spinet中所做的那样......

1 个答案:

答案 0 :(得分:2)

您可以使用AppDomain.CreateInstanceFromAndUnwrap()的重载定义来接受其他参数,一组对象将传递给其中的构造函数。您的代码可能如下所示:

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