Andrey Bushman
Andrey Bushman

Reputation: 12516

How to transfer constructor parameters to AppDomain.CreateInstanceXXX?

My class has no default parameterless constructor. It has such a constructor instead:

public Section(string fileName) {...}

I am to create an instance of my class inside of some AppDomain. If my class had a default constructor, then I would do it like this:

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

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

But there is no default constructor. How can I transfer the parameters of my constructor?

I expected it to work something like this:

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

But this doesn't work.

Upvotes: 7

Views: 2747

Answers (1)

user47589
user47589

Reputation:

You need to use this overload:

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

Upvotes: 10

Related Questions