【问题标题】:How to transfer constructor parameters to AppDomain.CreateInstanceXXX?如何将构造函数参数传递给 AppDomain.CreateInstanceXXX?
【发布时间】:2016-10-26 13:56:23
【问题描述】:

我的类没有默认的无参数构造函数。它有这样一个构造函数:

public Section(string fileName) {...}

我要在一些AppDomain 中创建我的类的一个实例。如果我的类有一个默认构造函数,那么我会这样做:

AppDomain domain = AppDomain.CreateDomain("ACAD-0001:409");

ISection section = (ISection)domain.CreateInstanceAndUnwrap(
    typeof(Section).Assembly.FullName, typeof(Section).FullName);

但是没有默认构造函数。如何传递构造函数的参数?

我希望它能够像这样工作:

string cuiFile = "...";
ISection section = (ISection)domain.CreateInstanceAndUnwrap(
    typeof(Section).Assembly.FullName, 
    typeof(Section).FullName, 
    new object[] { cuiFile });

但这不起作用。

【问题讨论】:

标签: c# .net c#-4.0 appdomain remoting


【解决方案1】:

你需要使用this overload:

public object CreateInstanceAndUnwrap(
    string assemblyName,
    string typeName,
    bool ignoreCase,
    BindingFlags bindingAttr,
    Binder binder,
    object[] args,            // <-- args go here
    CultureInfo culture,
    object[] activationAttributes
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2015-10-11
    相关资源
    最近更新 更多