How to transfer constructor parameters to AppDomain.CreateInstanceXXX?
Asked Answered
E

1

7

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.

Elbertine answered 26/10, 2016 at 13:56 Comment(1)
msdn.microsoft.com/pl-pl/library/dd414842(v=vs.110).aspx - note that there are TWO object[] - one is ARGS you wanted, the other is attributesRachitis
G
10

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
)
Gelatinate answered 26/10, 2016 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.