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.

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • https://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 attributes – quetzalcoatl Oct 26 '16 at 14:09

1 Answers1

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
)